一、JSON的定义(摘自 http://www.json.org/)
JSON (JavaScript Object Notation)is a lightweight(轻量级)data-interchange(数据交换) format(格式).It is easy for humans to read and write.It is easy for machines(机器) to parse(解析)and generate(生成) .It is based on a subset(子集)of the JavaScript Programming Language,Standard(标准的)ECMA-262 3rd Edition - December 1999.JSON is a text format(文本格式) that is completely (完全)language independent(独立) but uses conventions(习惯,习俗) that are familiar to programmers of the C-family of languages,including C,C++,C#,Java,JavaScript,Perl,Python,and many others.These properties(特性) make JSON an ideal(理想的) data-interchange language.
JSON is built on two structures:(JSON基于两种结构:)
These are universal(常见的,通用的) data structures. Virtually(几乎) all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable(可交换的) with programming languages also be based on these structures.
In JSON, they take on these forms(形式):
An object(对象) is an unordered set(集合) of name/value pairs. An object begins with “{”(left brace左括号) and ends with “}” (right brace右括号). Each name is followed by “:” (colon 冒号) and the name/value pairs(对) are separated(分隔) by “,” (comma 逗号).格式例如:{“key1”:value1,"key2":value2,"key3":value3,...}.
An array(数组) is an ordered collection of values. An array begins with “[” (left bracket) and ends with “]” (right bracket). Values are separated by “,” (comma).格式例如:[{“key1”:value1,"key2":value2,"key3":value3},{“key1”:value1,"key2":value2,"key3":value3},...].
A value can be a string in double quotes“引号”, or a number, or true or false or null, or an object or an array. These structures can be nested(嵌套).
A string is a sequence(序列) of zero or more Unicode characters(字符), wrapped in double quotes, using backslash escapes(反斜杠). A character is represented as(被表示为) a single character string. A string is very much like a C or Java string.
A number is very much like a C or Java number, except that the octal(八进制) and hexadecimal(十六进制) formats are not used.
Whitespace can be inserted between any pair of tokens(符号). Excepting a few encoding(编码) details(细节), that completely(完全) describes(描述) the language.
小结:JSON的值可以是:数字(整数或浮点数)、字符串(在双引号中)、逻辑值(true或false)、null、数组(在方括号中)、对象(在花括号中)。
二、JSON笔记
1、JSON指的是JavaScript对象表示法(Java Script Object Notation)
2、JSON是轻量级的文本数据交换格式。
3、JSON独立于语言:JSON使用JavaScript语法来描述数据对象,但是JSON仍然独立于语言和平台,JSON解析器和JSON库支持许多不同的编程语言,目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON。
4、JSON具有自我描述性,更易于理解。
注:JSON并不是一个文档格式,没有*.json的文档,一般JSON格式的文档存在txt中,而XML可以是一个标准。
5、JSON与XML的相同之处
(1)JSON是纯文本
(2)JSON具有“自我描述性”(人类可读)
(3)JSON具有层级结构(值中存在值)
(4)JSON可通过JavaScript进行解析
(5)JSON数据可使用AJAX(X指的是XML)进行传输
6、JSON与XML不同之处
(1)没有结束标签
(2)更短
(3)读写的速度更快
(4)能够使用内建的JavaScript eval()方法进行解析
(5)使用数组
(6)不使用保留字
注:任何的XML都能够转换成JSON包。XML中的属性也是通过JSON的Map表示。
7、为什么使用JSON
对于AJAX应用程序来说,JSON比XML更快更易使用;
使用XML
(1)读取XML文档
(2)使用XML DOM来循环遍历文档
(3)读取值并存储在变量中
使用JSON
(1)读取JSON字符串
(2)用eval()处理JSON字符串
四、如何在浏览器上查看JSON数据
1、页面-右击-查看元素-网络-(类型下面的)json
2、F12-网络-json