1. 程式人生 > >JSON語法介紹

JSON語法介紹

  官網:https://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(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