1. JSON数据格式

JSON(JavaScript Object Notation)

从结构上看,所有的数据最终都可以分成三种类型: 
第一种类型是scalar(标量),也就是一个单独的string(字符串)或数字(numbers),比如“北京”这个单独的词。 
第二种类型是sequence(序列)/array(数组)/List(列表),称作JSON数组,也就是若干个相关的数据按照一定顺序并列在一起,比如“北京,东京”。

第三种类型是mapping(映射)/hash(散列)/dictionary(字典),称作JSON对象,也就是一个名/值对(Name/value),在JSON中称作“对象”比如“首都:北京”。

可以作以下理解:

1.数组用“[]”创建,对象用“{}”创建;

2.无论是数组还是对象,之间的元素都用“,”隔开;

3.对象内部,(属性的)名称和值用“:”隔开,并且必须要用“:”隔开,不可单独存在属性名或者值;

4.对象和数组可以互相嵌套

JSON是以文本,即字符串的形式传递的,可以把JSON理解为内容需要满足特定格式书写的字符串。而在各种编程语言中,我们要将JSON字符串转换成各种语言中的变量,进行操作。以下就将一一介绍。

2.
PHP中使用JSON

2.1 PHP构造JSON(即,将PHP对象转化成JSON字符串)

例1:create
json object

  1. $info = array('number'=>'2','names'=>array('Everyday Updates','Hot Pictures','Latest Technology','Information','Product Pictures','Brands'));
  2. $json = json_encode($info);

结果:

{"number":"2","names":["Everyday Updates","Hot Pictures","Latest Technology","Information","Product Pictures","Brands"]}

例2:create json array

  1. $array = array (0 => "Eric", 1 => 23); echo json_encode($array);

程序将打印出 :["Eric",23]

注解:PHP中

1.数组,对象都用array创建,如果array中只有值,或者array键值对中的键是数字,则被转作JSON中的数组,如果array键值对中的键是字符串,那么被转作JSON中的对象;

2.无论是数组还是对象,之间的元素都用“,”隔开;

3.对象内部,(属性的)名称和值用“=>”隔开;

2.2 PHP使用JSON(即,将JSON字符串转化成PHP对象)

例1:

  1. $json = '{"foo-bar": 12345}';
  2. $obj = json_decode($json);
  3. print $obj->{'foo-bar'}; //

注解:在不知道JSON格式的时候,可以用var_dump(json_decode($str));来查看转换后的情况

3. JS中使用JSON

3.1 JS构造JSON字符串

  1. //法I:写成去掉引号的字符串
  2. var jsonObj = {number:6,names:[{name: DAILY UPDATES”}, {name: POPULAR PICTURES”}]};
  3. var jsonText = JSON.stringify(jsonObj);
  4.  
  5. //法II:适用于数组元素很多的情况,可以动态添加数组元素
  6. //get the request json data of the client
  7. var totalNumOfPictures = 17;
  8. var numInOnePage = req.body.number; //number of pictures in one page
  9. var startIndex = req.body.startIndex;
  10.  
  11. // create picture array
  12. var pictures = [];
  13. var picture = new Object();
  14. picture.name = "Vol.1";
  15. picture.url = "http://t2.baidu.com/it/u=1790018557,3075089305&fm=21&gp=0.jpg";
  16. pictures.push(picture); //create array dynamically
  17.  
  18. picture = new Object();
  19. picture.name = "Vol.2";
  20. picture.url = "http://t2.baidu.com/it/u=2602371353,2521141668&fm=21&gp=0.jpg";
  21. pictures.push(picture);
  22.  
  23. picture = new Object();
  24. picture.name = "Vol.3";
  25. picture.url = "http://t3.baidu.com/it/u=2280986867,1567705443&fm=21&gp=0.jpg";
  26. pictures.push(picture);
  27.  
  28. picture = new Object();
  29. picture.name = "Vol.4";
  30. picture.url = "http://t3.baidu.com/it/u=1821458190,3818067678&fm=21&gp=0.jpg";
  31. pictures.push(picture);
  32.  
  33. picture = new Object();
  34. picture.name = "Vol.5";
  35. picture.url = "http://t2.baidu.com/it/u=472744134,2285846599&fm=21&gp=0.jpg";
  36. pictures.push(picture);
  37.  
  38. picture = new Object();
  39. picture.name = "Vol.6";
  40. picture.url = "http://t2.baidu.com/it/u=3957952154,3084688128&fm=21&gp=0.jpg";
  41. pictures.push(picture);
  42.  
  43. picture = new Object();
  44. picture.name = "Vol.7";
  45. picture.url = "http://t1.baidu.com/it/u=2291760722,2270589689&fm=21&gp=0.jpg";
  46. pictures.push(picture);
  47.  
  48. picture = new Object();
  49. picture.name = "Vol.8";
  50. picture.url = "http://t3.baidu.com/it/u=2759709517,445035895&fm=21&gp=0.jpg";
  51. pictures.push(picture);
  52.  
  53. picture = new Object();
  54. picture.name = "Vol.9";
  55. picture.url = "http://t1.baidu.com/it/u=812989780,2797001786&fm=23&gp=0.jpg";
  56. pictures.push(picture);
  57.  
  58. picture = new Object();
  59. picture.name = "Vol.10";
  60. picture.url = "http://i3.hunantv.com/p1/20121127/1058553836.jpg";
  61. pictures.push(picture);
  62.  
  63. picture = new Object();
  64. picture.name = "Vol.11";
  65. picture.url = "http://img.21cbh.com/uploadfile/2012/0824/20120824013832913.jpg";
  66. pictures.push(picture);
  67.  
  68. picture = new Object();
  69. picture.name = "Vol.12";
  70. picture.url = "http://i3.cnfolimg.com/auto/3_906048.jpg";
  71. pictures.push(picture);
  72.  
  73. picture = new Object();
  74. picture.name = "Vol.13";
  75. picture.url = "http://pic.yesky.com/imagelist/08/12/8093046_5924.jpg";
  76. pictures.push(picture);
  77.  
  78. picture = new Object();
  79. picture.name = "Vol.14";
  80. picture.url = "http://www.autotimes.com.cn/upload/admin/images/chezhan/beijing/2012/model/bsj-5.jpg";
  81. pictures.push(picture);
  82.  
  83. picture = new Object();
  84. picture.name = "Vol.15";
  85. picture.url = "http://i3.cqnews.net/news/attachement/jpg/site82/2011-04-29/5356167799388589428.jpg";
  86. pictures.push(picture);
  87.  
  88. picture = new Object();
  89. picture.name = "Vol.16";
  90. picture.url = "http://img.shendu.com/forum/201212/27/115921b05wp2ah1sg46bqa.jpg";
  91. pictures.push(picture);
  92.  
  93. picture = new Object();
  94. picture.name = "Vol.17";
  95. picture.url = "http://www.atv.com.cn/bbs/data/attachment/forum/201105/28/160747a1ydwwcccwyhjuhj.jpg";
  96. pictures.push(picture);
  97.  
  98. picture = new Object();
  99. picture.name = "Vol.18";
  100. picture.url = "http://jpp2.imghb.com/pic/pic/12/37/21/1423016512372180_a602x602.jpg";
  101. pictures.push(picture);
  102.  
  103. //create json data
  104. var json = new Object();
  105. json.numOfPages = 2;
  106. json.number = numInOnePage;
  107.  
  108. var pictureJson = [];
  109. for(var i = startIndex; i < startIndex+json.number; i++)
  110. {
  111. pictureJson.push(pictures[i]);
  112. }
  113. json.pictures = pictureJson;
  114.  
  115. if(req.body.id==100){
  116. res.send(JSON.stringify(json));
  117. }

3.2 JSON字符串转成JS对象

JSON字符串:

  1. var str1 = '{ "name": "cxh", "sex": "man" }';

JS对象:

  1. var str2 = { "name": "cxh", "sex": "man" };

由JSON字符串转换为JS对象

(法I)  var obj = eval_r('(' + str + ')');

解释:由于json是以”{}”的方式来开始以及结束的,在JS中,它会被当成一个语句块来处理,所以加上圆括号的目的是迫使eval函数在处理JavaScript代码的时候强制将括号内的表达式(expression)转化为对象,而不是作为语 句(statement)来执行。

(法II) var obj = str.parseJSON();

(法III) var obj = JSON.parse(str);

然后str.name

4. Java中使用JSON

4.1 JAVA构造JSON字符串

写成string就行

  1. String requestContent = "{"id":"1","sort":"des","number":"9","startIndex":"1"}";
  2. try {
  3. StringEntity entity = new StringEntity(requestContent);
  4. entity.setContentEncoding("UTF-8");
  5. entity.setContentType("application/json"); //set the request content type as JSON
  6. httpPost.setEntity(entity);
  7. } catch (UnsupportedEncodingException e1) {
  8. e1.printStackTrace();
  9. }
  10. httpResponse=httpClient.execute(httpPost);

4.2 将JSON字符串转化成Java对象(JSON Object 或者JSONArray)

  1. //如果是{
  2. JSONObject obj = new JSONObject(response);
  3. //如果class后面是=>[
  4. JSONArray array = obj.getJSONArray("class");
  5. //从JSONArray获得JSONObject
  6. JSONObject obj = array.getJSONObject(0);

判断键/值对是否存在

  1. if ( jObj.has("identity") ) ...

 

JSON在各种语言中的运用的更多相关文章

  1. Swift语言中如何使用JSON数据教程

    这是一篇翻译文章,原文出处:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial Swift语言中如何使用JSO ...

  2. PHP语言中使用JSON

    原文地址:http://www.ruanyifeng.com/blog/2011/01/json_in_php.html 在PHP语言中使用JSON 目前,JSON已经成为最流行的数据交换格式之一,各 ...

  3. 在 Swift 语言中更好的处理 JSON 数据:SwiftyJSON

    SwiftyJSON能够让在Swift语言中更加简便处理JSON数据. With SwiftyJSON all you have to do is: ? 1 2 3 4 let json = JSON ...

  4. (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解   本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创 ...

  5. go 语言中常用的包

    来自学习go语言.pdf 译者刑星 ==== fmt 包fmt实现了格式化IO函数,这与c的printf和scanf类似,格式化短语派生于c %v 默认格式的值.当打印结构时,加号(%+v)会增加字段 ...

  6. 在 go/golang语言中使用 google Protocol Buffer

    怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{ ...

  7. 在Go语言中基础的Redis操作

    在Go语言中基础的Redis操作 需要先安装redigo go get "github.com/garyburd/redigo/redis" Go语言Redis客户端的简单示例 连 ...

  8. asp.net core 基于 JSON 实现多语言

    asp.net core 基于 JSON 实现多语言 Intro 上次我们提到了,微软默认提供基于资源文件的多语言本地化,个人感觉使用起来不是太方便,没有 json 看起来直观,于是动手造了一个轮子, ...

  9. JAVA语言中的修饰符

    JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...

随机推荐

  1. LINK : fatal error LNK1158: 无法运行“rc.exe”解决办法 and Visual Studio 2017 下载安装

    LINK : fatal error LNK1158: 无法运行“rc.exe” 首先下载软件包:https://pan.baidu.com/s/1L1N1sikXUaZZd-9nmZnwjA 第一个 ...

  2. Docker 1

    1.Docker安装 下载地址:https://download.docker.com/mac/edge/Docker.dmg 验证Docker信息 docker --version docker-c ...

  3. Linux下几种反弹Shell方法的总结与理解

    之前在网上看到很多师傅们总结的linux反弹shell的一些方法,为了更熟练的去运用这些技术,于是自己花精力查了很多资料去理解这些命令的含义,将研究的成果记录在这里,所谓的反弹shell,指的是我们在 ...

  4. Scrapyd发布爬虫的工具

    Scrapyd Scrapyd是部署和运行Scrapy.spider的应用程序.它使您能够使用JSON API部署(上传)您的项目并控制其spider. Scrapyd-client Scrapyd- ...

  5. event事件传播规则

    参考原文:https://my.oschina.net/u/1454562/blog/205010 event事件传播有三个阶段:捕获阶段.目标阶段.冒泡阶段 target.addEventListe ...

  6. MySql 链接字符串

    MySql连接字符串总结 1.本地数据库连接    <connectionStrings>        <add name="ConnectionString" ...

  7. pdb调试工具

    调试--pdb pdb是基于命令行的调试工具,非常类似gnu的gdb(调试c/c++). 命令 简写命令 作用 break b 设置断点 continue c 继续执行程序 list l 查看当前行的 ...

  8. bootstrap3中模态框的数据编辑使用方法

    模态框是bootstrap3中比较好用得弹窗控件,这回使用了 说主要的,官方详细教程 http://www.runoob.com/bootstrap/bootstrap-modal-plugin.ht ...

  9. js选择器 querySelector

    <form method="post" action="" id="myform"> <input type=" ...

  10. 不丢失log的情况下迁移git空间

    背景: 原来的源码都放在个人的空间下,为了团队后续规范管理,建立了一个团队使用的空间,需要把旧空间的文件和提交记录,一起迁移. 方法:git remote 进行空间切换 1.查看当前空间名:git r ...