JSON在各种语言中的运用
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在各种语言中的运用的更多相关文章
- Swift语言中如何使用JSON数据教程
这是一篇翻译文章,原文出处:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial Swift语言中如何使用JSO ...
- PHP语言中使用JSON
原文地址:http://www.ruanyifeng.com/blog/2011/01/json_in_php.html 在PHP语言中使用JSON 目前,JSON已经成为最流行的数据交换格式之一,各 ...
- 在 Swift 语言中更好的处理 JSON 数据:SwiftyJSON
SwiftyJSON能够让在Swift语言中更加简便处理JSON数据. With SwiftyJSON all you have to do is: ? 1 2 3 4 let json = JSON ...
- (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解 本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创 ...
- go 语言中常用的包
来自学习go语言.pdf 译者刑星 ==== fmt 包fmt实现了格式化IO函数,这与c的printf和scanf类似,格式化短语派生于c %v 默认格式的值.当打印结构时,加号(%+v)会增加字段 ...
- 在 go/golang语言中使用 google Protocol Buffer
怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{ ...
- 在Go语言中基础的Redis操作
在Go语言中基础的Redis操作 需要先安装redigo go get "github.com/garyburd/redigo/redis" Go语言Redis客户端的简单示例 连 ...
- asp.net core 基于 JSON 实现多语言
asp.net core 基于 JSON 实现多语言 Intro 上次我们提到了,微软默认提供基于资源文件的多语言本地化,个人感觉使用起来不是太方便,没有 json 看起来直观,于是动手造了一个轮子, ...
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
随机推荐
- LINK : fatal error LNK1158: 无法运行“rc.exe”解决办法 and Visual Studio 2017 下载安装
LINK : fatal error LNK1158: 无法运行“rc.exe” 首先下载软件包:https://pan.baidu.com/s/1L1N1sikXUaZZd-9nmZnwjA 第一个 ...
- Docker 1
1.Docker安装 下载地址:https://download.docker.com/mac/edge/Docker.dmg 验证Docker信息 docker --version docker-c ...
- Linux下几种反弹Shell方法的总结与理解
之前在网上看到很多师傅们总结的linux反弹shell的一些方法,为了更熟练的去运用这些技术,于是自己花精力查了很多资料去理解这些命令的含义,将研究的成果记录在这里,所谓的反弹shell,指的是我们在 ...
- Scrapyd发布爬虫的工具
Scrapyd Scrapyd是部署和运行Scrapy.spider的应用程序.它使您能够使用JSON API部署(上传)您的项目并控制其spider. Scrapyd-client Scrapyd- ...
- event事件传播规则
参考原文:https://my.oschina.net/u/1454562/blog/205010 event事件传播有三个阶段:捕获阶段.目标阶段.冒泡阶段 target.addEventListe ...
- MySql 链接字符串
MySql连接字符串总结 1.本地数据库连接 <connectionStrings> <add name="ConnectionString" ...
- pdb调试工具
调试--pdb pdb是基于命令行的调试工具,非常类似gnu的gdb(调试c/c++). 命令 简写命令 作用 break b 设置断点 continue c 继续执行程序 list l 查看当前行的 ...
- bootstrap3中模态框的数据编辑使用方法
模态框是bootstrap3中比较好用得弹窗控件,这回使用了 说主要的,官方详细教程 http://www.runoob.com/bootstrap/bootstrap-modal-plugin.ht ...
- js选择器 querySelector
<form method="post" action="" id="myform"> <input type=" ...
- 不丢失log的情况下迁移git空间
背景: 原来的源码都放在个人的空间下,为了团队后续规范管理,建立了一个团队使用的空间,需要把旧空间的文件和提交记录,一起迁移. 方法:git remote 进行空间切换 1.查看当前空间名:git r ...