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") ) ...

 

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. shell 8printf

    printf printf使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度.左右对其方式等.printf不会像echo自动添加换行符,因此需要手动添加\n ...

  2. Linux故障-bash-4.1$

    #模拟故障:-bash-4.1$ [root@nodchen ~]# su - cisco[cisco@nodchen ~]$ \rm -f .*rm: cannot remove `.': Is a ...

  3. php history.back返回后表单数据丢失的解决办法

    js使用history.back返回表单数据丢失的主要原因就是使用了session_start();的原因,该函数会强制当前页面不被缓存.本文章向码农介绍php history.back返回后表单数据 ...

  4. 管道(pipe),进程之间的共享内存(Manager,Value)

    1 管道(了解) from multiprocessing import Pipe con1,con2 = Pipe() 管道是不安全的. 管道是用于多进程之间通信的一种方式. 如果在单进程中使用管道 ...

  5. dos批处理文件中的变量小结

    很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友   批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...

  6. PyQt5系列教程(九)QInputDialog的使用

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 输入框是界面开发中非常常见的控件,本文就来看看PyQt5中QInputDialog的使用 实例 ...

  7. mavenProfile文件配置和简单入门

    1什么是MavenProfile 在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产 ...

  8. 重识linux-RPM命令

    最近开始系统的学习linux,买了本 鸟哥的私房菜,当工具书  系统的看看 学习 多练习 才是王道 RPM是个很强大的命令 全称是 Redhat Package Management   红帽包管理工 ...

  9. c# 导入导出excel表格式

    c#使用代码导入excel时,当遇到纯数字且大于15位时会出现编码混乱(表现为科学计数法),要想呈现与excel表中纯数字格式和在数据库中呈现纯数字,操作如下: 完成即可. 导出取决于导入的内容排版.

  10. SparkSession

    在2.0版本之前,使用Spark必须先创建SparkConf和SparkContext catalog:目录 Spark2.0中引入了SparkSession的概念,SparkConf.SparkCo ...