JSON语法介绍
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(JavaScript Object Notation)是一种轻量级的数据交换格式。 人类很容易读写。 机器很容易解析和生成。 它基于JavaScript编程语言的一个子集,标准ECMA-262第3版 - 1999年12月.JSON是一种完全独立于语言的文本格式,但使用C语言系列程序员熟悉的约定,包括C语言 ,C ++,C#,Java,JavaScript,Perl,Python等等。 这些属性使JSON成为理想的数据交换语言。
JSON is built on two structures:
* A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
* An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
译:
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:
1.
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).
译:
在JSON中,他们采用以下形式:
对象是一组无序的名称/值对。 对象以{(左括号)开头,以}结尾(右括号)。 每个名称后跟:(冒号),名称/值对用(逗号)分隔。
![](https://www.json.org/object.gif)
总结:用花括号表示对象,名称/值对可以有多个,并用逗号分隔
示例:
{ "firstName":"John" , "lastName":"Doe" }
一个{}就是一个JSONObject
2.
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
译:
数组是有序的值集合。 数组以[(左括号)开头并以]结尾(右括号)。 值以(逗号)分隔。
![](https://www.json.org/array.gif)
总结:用方括号表示数组,值以逗号分隔
示例:
数组可包含多个对象
{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
说明:employees的值是一个数组,数组的元素是3个对象,每个对象是名称/值对,3个对象之间以逗号分隔
3.
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.
译:
值可以是双引号中的字符串,数字,或true或false或null,或对象或数组。 这些结构可以嵌套。
![](https://www.json.org/value.gif)
JSON 值可以是:
- 数字(整数或浮点数)
- 字符串(在双引号中)
- 逻辑值(true 或 false)
- 数组(在方括号中)
- 对象(在花括号中)
- null
- JSONObject
- JSONArray
4.
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.
译:
字符串是零个或多个Unicode字符的序列,用双引号括起来,使用反斜杠转义。 字符表示为单个字符串。 字符串非常类似于C或Java字符串。
![](https://www.json.org/string.gif)
总结:使用反斜杠转义。
5.
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
译:
除非不使用八进制和十六进制格式,否则数字非常类似于C或Java编号。
![](https://www.json.org/number.gif)
JSON Vs XML
1.JSON和XML的数据可读性基本相同
2.JSON和XML同样拥有丰富的解析手段
3.JSON相对于XML来讲,数据的体积小
4.JSON与JavaScript的交互更加方便
5.JSON对数据的描述性比XML较差
6.JSON的速度要远远快于XML