`
schy_hqh
  • 浏览: 543924 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

JSON

 
阅读更多

JSON的数据结构

JSON中可以使用的数据类型

在线查看JSON中定义的数据

JSON格式校验

JSON与Javascript进行组合

使用JSON Path查询特定数据

使用JSONP完成跨域访问

JSON的变体-MongoDB中存储数据所用的BSON

 

JSON is a lightweight text-based open standard data-interchange format. It is human readable. JSON is derived from a subset of JavaScript programming language (Standard ECMA-262 3rd Edition—December 1999). It is entirely language independent and can be used with most of the modern programming languages.

JSON是一个轻量级的基于文本的标准数据交换格式。可读性极佳。

JSON源于javascript编程语言的一个子集所发展而来。

它不依赖于任何语言的开发环境,并且被大多数现代编程语言所支持的。

 

JSON is often used to serialize and transfer data over a network connection, for example between web server and a web application. In computer science, serialization is a process to transforming data structures and objects in a format suitable to be stored in a file or memory buffer or transmitted over a network connection. Later on, this data can be retrieved. Because of the very nature of the JSON, it is useful for storing or representing semi structured data.

JSON通常被用来在网络上通过序列化完成传输功能。

在计算机科学中,序列化指的是转换数据结构和使用合适的格式将对象存储到文件或者内存缓冲区或者在网络上传输。

以后,该数据能够被还原。因为JSON所采用的完全自然的表达方式,使其有利于保存或重现结构化的数据。

 

 

JSON files are saved with .json extension. Internet media type of JSON is "application/json".

JSON扩展名: .json

MIME类型:application/json

 

JSON的组成:

key:字符串

value:

基本类型:字符串("Kidle Edition")、数字(1.20)、布尔值(true/false)、null

结构化数据类型:对象({ })、数组([ ])

 


 

 

 

=======================================================================

 

JSON uses Object and Array as data structures and strings, number, true, false and null as vales. Objects and arrays can be nested recursively. - See more at: http://www.w3resource.com/JSON/introduction.php#sthash.AtGiLMQ5.dpuf

JSON uses Object and Array as data structures and strings, number, true, false and null as vales.

Objects and arrays can be nested recursively

对象与数组嵌套完成数据定义

 

{
    "firstName": "Bidhan",
    "lastName": "Chatterjee",
    "age": 40,
    "address": {
        "streetAddress": "144 J B Hazra Road",
        "city": "Burdwan",
        "state": "Paschimbanga",
        "postalCode": "713102"
    },
    "phoneNumber": [
        {
            "type": "personal",
            "number": "09832209761"
        },
        {
            "type": "fax",
            "number": "91-342-2567692"
        }
    ]
}

 

JSON中合法的Value

String || Number || Object || Array || TRUE || FALSE || NULL
A value can be a string, a number, an object, an Array, a Boolean value (i.e. true or false) or Null. This structure can be nested.
字符串 || 数字 || 对象 || 数组 || TRUE || FALSE || NULL

对象与数组可以嵌套

 

在线查看/格式化JSON中定义的数据

http://jsoneditoronline.org/

http://jsonviewer.stack.hu/

http://chris.photobooks.com/json/default.htm

 

JSON格式正确性校验

http://jsonlint.com/

http://www.bejson.com

 

JSON与Javascript进行组合

What is serialize and deserialize

Get JSON value from JavaScript value is serialization and when it's other way (JSON to JavaScript) is deserialization. serialization: 将javascript中的对象转换为JSONdeserialization: 将JSON转换为javascript中的对象

 

JavaScript JSON object

The JavaScript JSON object comprises methods using which you can convert JavaScript values to JSON format and JSON notation to JavaScript values.

Javascript中的JSON对象包含这样的方法:

JSON.stringify:将Javascript中定义的变量转换为JSON格式的数据

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON.stringify</title>
</head>
<body>
	<script type="text/javascript">
		var w3r = {};
		w3r.name = "w3resource";
		w3r.ip = "201.2.31.82";
		//将javascript对象转换为json数据
		var jsonStr = JSON.stringify(w3r);
		document.write(typeof jsonStr);
		document.write(": ");
		document.write(jsonStr);
		
		//output:
		//string:{"name":"w3resource","ip":"201.2.31.82"} 
	</script>
</body>
</html>

 

JSON.parse:将JSON格式的数据转换为Javascript中的对象

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON.stringify</title>
</head>
<body>
	<script type="text/javascript">
		var jsonStr = '{"name":"w3resource","ip":"201.2.31.82"}';
		var converToObj = JSON.parse(jsonStr);
		document.write("name=");
		document.write(converToObj.name);
		document.write(", ip=");
		document.write(converToObj.ip);
		//output:
		//name=w3resource, ip=201.2.31.82 
	</script>
</body>
</html>

 

 

使用JSON Path查询特定数据

要使用jsonPath进行查找,需要引入2个额外的js:json.js, jsonpath.js (见附件)

jsonPath(obj, expr [, args])

obj: 由json转换得到的对象

expr:查询字符串(http://goessner.net/articles/JsonPath/)

[,args]: 暂时不学

 

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}

 

XPath JSONPath Result
/store/book/author $.store.book[*].author the authors of all books in the store
//author $..author all authors
/store/* $.store.* all things in store, which are some books and a red bicycle.
/store//price $.store..price the price of everything in the store.
//book[3] $..book[2] the third book
//book[last()] $..book[(@.length-1)]
$..book[-1:]
the last book in order.
//book[position()<3] $..book[0,1]
$..book[:2]
the first two books
//book[isbn] $..book[?(@.isbn)] filter all books with isbn number
//book[price<10] $..book[?(@.price<10)] filter all books cheapier than 10
//* $..* all Elements in XML document. All members of JSON structure.

 

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>
			Insert title here
		</title>
	</head>
	<body>
		<script type="text/javascript" src="json.js"></script>
		<script type="text/javascript" src="jsonpath.js"></script>
		<script type="text/javascript">
			var json = {
				"MovieDatabase": {
					"movie": [{
						"name": "The Change-Up",
						"genre": "comedy",
						"director": "David Dobkin",
						"Facebook_like": 252
					},
					{
						"name": "Rise of the Planet of the Apes",
						"genre": "SciFi",
						"director": "Rupert Wyatt",
						"Facebook_like": 472
					},
					{
						"name": "30 Minutes or Less",
						"genre": "adventure",
						"director": "Ruben Fleischer",
						"Facebook_like": 114
					},
					{
						"name": "Final Destination 5",
						"genre": "Horror",
						"director": "Steven Quale",
						"Facebook_like": 241
					}]
				}
			}
		</script>
		
		<script type="text/javascript">
			var result = "";
			result = jsonPath(json, "$.MovieDatabase.movie[*].name").toJSONString();
			document.write(result);
			//output
			//["The Change-Up","Rise of the Planet of the Apes","30 Minutes or Less","Final Destination 5"] 
		</script>
	</body>

</html>

 

 

 

使用JSONP完成跨域请求

JSONP is used to request data from a server residing in a different domain. But why do we need a special technique to access data from a different domain? It's because of the Same Origin Policy.

Same Origin Policy 同源策略

In general, this policy states that, if protocol (like http), Port number (like 80) and host (like example.com) is different from where data is being requested, it should not be permitted.
But HTML <script> element is allowed to perform content retrieval from foreign origins.

 

JSONP is mostly used to get data using RESTFull APIs:

比如从 Flicker图片分享网站获取图片

http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?

 

 

<!DOCTYPE html>
<html>
	<head>
		<style>
			img{ height: 100px; float: left; }
		</style>
		<script src="http://code.jquery.com/jquery-latest.js">
		</script>
		<title>
			An JSONP example from w3resource
		</title>
	</head>
	<body>
		<div id="images">
		</div>
		<script>
			$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {
				tags: "dogs",
				tagmode: "any",
				format: "json"
			},
			function(data) {
				$.each(data.items,
				function(i, item) {
					$("<img/>").attr("src", item.media.m).appendTo("#images");
					if (i == 3) return false;
				});
			});
		</script>
	</body>

</html>
 

 

 

 

JSON的变体-MongoDB中存储数据所用的BSON

 

  • 大小: 49.4 KB
  • 大小: 20.1 KB
分享到:
评论

相关推荐

    最好用的c++json库 nlohmann json源代码

    最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json库 nlohmann json源代码最好用的c++json...

    C# json格式解析,Json格式字符串与C#对象相互转换,类库+使用案例,注释详细

    C# json格式转换,Json格式字符串与C#对象相互转换,类库和测试demo 写了一个json与C#对象相互装换的类库,直接调用就行,有测试案例,代码注释非常详细 部分方法: /// 将Json字符串解析为C#中的对象 /// Json格式...

    pb通过http协议传json.zip

    pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb通过http协议传json; pb...

    C# JSON 编码解码

    //Program.cs 里是一些比较极端的测试,实际使用时只需复制 JSON.cs 到项目里。 //比如类Vec2: public class Vec2 { public float x; public float y; } //编码范例: Vec2 v1 = new Vec2(); v1.x = 1.23f; v1....

    json2toml:将JSON转换为TOML

    json2toml 将JSON转换为 。例子var json2toml = require ( 'json2toml' ) ;json2toml ( { simple : true } ) ;// =&gt; 'simple = true\n'// Also supports pretty-printing optionsjson2toml ( { deeply : { option : ...

    C#.Net2.0解析Json,精简版的Newtonsoft.Json.dll,JsonReader,JsonSerializer(Json.Net)

    基于Newtonsoft.Json精简的。.Net2.0的哦 仅保留了读取和解析json数据的相关类和方法(JsonReader,JsonSerializer),去除了写入json数据以及json和xml互相转换的部分以及其他不常用的类。 编译后dll仅20kb 也可以直接...

    mysql 解析json字符串

    mysql解析Json字符串插件 安装方法 1、拷贝lib_mysqludf_json_parsing.dll到mysql目录C:\Program Files\MariaDB 5.5\lib\plugin下 2、在数据库中执行 DROP FUNCTION json_get; CREATE FUNCTION json_get RETURNS ...

    电视源json电视源json电视源json电视源json

    电视源json

    中国全国城市列表JSON数据2022最新

    中国城市列表JSON数据,中国所有城市,中国省市列表 2022最新json数据,2022年8月9日更新,因为最近需要用到中国省市列表的JSON数据 本来想把县也包含进去的,但是数量太多了~ 中国总共有23个省、5个自治区、4个直辖...

    全国省市区县街道json格式数据信息

    全国省市区县街道json: 文件含义 文件名称 省份数据 provinces.json 城市数据 cities.json 区县数据 areas.json 乡镇(街道)数据 streets.json “省份、城市” 二级联动数据 pc.json “省份、城市” 二级联动数据...

    json paser 属于idea插件 用于解析json

    json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于idea插件 用于解析json json paser 属于...

    Vue加载json文件的方法简单示例

    本文实例讲述了Vue加载json文件的方法。分享给大家供大家参考,具体如下: 一、在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): var appData = require('../address.json'); // ...

    android解析通过http返回的json数据 包括服务器json数据

    1. 例子代码有两部分:androidJson是andoird的工程,inxdex.php是php服务器json数据产生部分代码。 2. 服务器端提供http(get)获取方式获取json数据的api例子,是用php写的。 4. android的apk给你说完成http数据...

    MFC使用json11解析JSON

    MFC使用JSON11将json,包括json字符串转对象,对象转json字符串

    全国省市区县街道json

    全国省市区县街道json: 文件含义 文件名称 省份数据 provinces.json 城市数据 cities.json 区县数据 areas.json 乡镇(街道)数据 streets.json “省份、城市” 二级联动数据 pc.json “省份、城市” 二级联动数据...

    2020年全国省市区县街道四级联动json编码.zip

    2020年全国省市区县街道四级联动json编码 真实可靠 文件含义 文件名称 省份数据 provinces.json 城市数据 cities.json 区县数据 areas.json 乡镇(街道)数据 streets.json ...

    Json离线格式化工具

    在开发中,如果用到Json传递或者存储数据,Newtonsoft.Json序列化后的内容很难阅读,Json格式化(树状结构)工具由此诞生。压缩包中包含一个美化工具(exe)、C# net 3.5的格式化dll和一个简单的demo文件(exe工具...

    json3.js 【JS / JavaScript 中解析JSON的js包,JSON官方的JSON解析包】

    json3.js 【JS / JavaScript 中解析JSON的js包,JSON官方的JSON解析包】。JavaScript中解析JSON的js包,页面中引入json3.js,即可使用。 使用方法:JSON.parse(str), JSON.stringify(obj) 更多详情请参考博文: ...

    i3-json-2010.rar_JSON_LabVIEW JSON_i3 json_labview与json_labview解

    json与labview常见相互转化,能解决大多json问题

    【精】JSON需要的所有jar包

    这里是json开发需要的所有jar包文件哦~还有精心的json封装方法提示和最新jar包下载地址~现在拿出来分享啦!希望对大家有帮助! (1)json-lib最新版本可以从这个地方下载:...

Global site tag (gtag.js) - Google Analytics