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中,他們采用以下形式:
 
對象是一組無序的名稱/值對。 對象以{(左括號)開頭,以}結尾(右括號)。 每個名稱后跟:(冒號),名稱/值對用(逗號)分隔
 
 
總結:用花括號表示對象,名稱/值對可以有多個,並用逗號分隔
 
示例:
{ "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).
 
譯:
數組是有序的值集合。 數組以[(左括號)開頭並以]結尾(右括號)。 值以(逗號)分隔
 
總結:用方括號表示數組,值以逗號分隔
 
示例:
數組可包含多個對象
{
"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,或對象或數組。 這些結構可以嵌套。
 
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字符串。
 
 
總結:使用反斜杠轉義。
 
5.
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
譯:
除非不使用八進制和十六進制格式,否則數字非常類似於C或Java編號。
 
 
JSON Vs XML
1.JSON和XML的數據可讀性基本相同
2.JSON和XML同樣擁有豐富的解析手段
3.JSON相對於XML來講,數據的體積小
4.JSON與JavaScript的交互更加方便
5.JSON對數據的描述性比XML較差
6.JSON的速度要遠遠快於XML
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM