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
$info = array('number'=>'2','names'=>array('Everyday Updates','Hot Pictures','Latest Technology','Information','Product Pictures','Brands'));
$json = json_encode($info);
结果:
{"number":"2","names":["Everyday Updates","Hot Pictures","Latest Technology","Information","Product Pictures","Brands"]}
例2:create json array
$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:
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; //
注解:在不知道JSON格式的时候,可以用var_dump(json_decode($str));来查看转换后的情况
3. JS中使用JSON
3.1 JS构造JSON字符串
//法I:写成去掉引号的字符串
var jsonObj = {number:6,names:[{name: “DAILY UPDATES”}, {name: “POPULAR PICTURES”}]};
var jsonText = JSON.stringify(jsonObj); //法II:适用于数组元素很多的情况,可以动态添加数组元素
//get the request json data of the client
var totalNumOfPictures = 17;
var numInOnePage = req.body.number; //number of pictures in one page
var startIndex = req.body.startIndex; // create picture array
var pictures = [];
var picture = new Object();
picture.name = "Vol.1";
picture.url = "http://t2.baidu.com/it/u=1790018557,3075089305&fm=21&gp=0.jpg";
pictures.push(picture); //create array dynamically picture = new Object();
picture.name = "Vol.2";
picture.url = "http://t2.baidu.com/it/u=2602371353,2521141668&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.3";
picture.url = "http://t3.baidu.com/it/u=2280986867,1567705443&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.4";
picture.url = "http://t3.baidu.com/it/u=1821458190,3818067678&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.5";
picture.url = "http://t2.baidu.com/it/u=472744134,2285846599&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.6";
picture.url = "http://t2.baidu.com/it/u=3957952154,3084688128&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.7";
picture.url = "http://t1.baidu.com/it/u=2291760722,2270589689&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.8";
picture.url = "http://t3.baidu.com/it/u=2759709517,445035895&fm=21&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.9";
picture.url = "http://t1.baidu.com/it/u=812989780,2797001786&fm=23&gp=0.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.10";
picture.url = "http://i3.hunantv.com/p1/20121127/1058553836.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.11";
picture.url = "http://img.21cbh.com/uploadfile/2012/0824/20120824013832913.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.12";
picture.url = "http://i3.cnfolimg.com/auto/3_906048.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.13";
picture.url = "http://pic.yesky.com/imagelist/08/12/8093046_5924.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.14";
picture.url = "http://www.autotimes.com.cn/upload/admin/images/chezhan/beijing/2012/model/bsj-5.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.15";
picture.url = "http://i3.cqnews.net/news/attachement/jpg/site82/2011-04-29/5356167799388589428.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.16";
picture.url = "http://img.shendu.com/forum/201212/27/115921b05wp2ah1sg46bqa.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.17";
picture.url = "http://www.atv.com.cn/bbs/data/attachment/forum/201105/28/160747a1ydwwcccwyhjuhj.jpg";
pictures.push(picture); picture = new Object();
picture.name = "Vol.18";
picture.url = "http://jpp2.imghb.com/pic/pic/12/37/21/1423016512372180_a602x602.jpg";
pictures.push(picture); //create json data
var json = new Object();
json.numOfPages = 2;
json.number = numInOnePage; var pictureJson = [];
for(var i = startIndex; i < startIndex+json.number; i++)
{
pictureJson.push(pictures[i]);
}
json.pictures = pictureJson; if(req.body.id==100){
res.send(JSON.stringify(json));
}
3.2 JSON字符串转成JS对象
JSON字符串:
var str1 = '{ "name": "cxh", "sex": "man" }';
JS对象:
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就行
String requestContent = "{"id":"1","sort":"des","number":"9","startIndex":"1"}";
try {
StringEntity entity = new StringEntity(requestContent);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json"); //set the request content type as JSON
httpPost.setEntity(entity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
httpResponse=httpClient.execute(httpPost);
4.2 将JSON字符串转化成Java对象(JSON Object 或者JSONArray)
//如果是{
JSONObject obj = new JSONObject(response);
//如果class后面是=>[
JSONArray array = obj.getJSONArray("class");
//从JSONArray获得JSONObject
JSONObject obj = array.getJSONObject(0);
判断键/值对是否存在
if ( jObj.has("identity") ) ...