JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式。易於人閱讀和編寫。同時也易於機器解析和產生。它基於JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集。JSON採用完全獨立於語言的文字格式設定,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。這些特性使JSON成為理想的資料交換語言。
-
object
-
{}
{
members
}
-
members
-
string
:
value
members
,
string
:
value
-
array
-
[]
[
elements
]
-
elements
-
value
elements
,
value
-
value
-
string
number
object
array
true
false
null
-
string
-
""
"
chars
"
-
chars
-
char
chars char
-
char
-
any-Unicode-except-
"
-or-
/
-or-control
/"
//
//
/b
/f
/n
/r
/t
/u
four-hex-digits
-
number
-
int
int frac
int exp
int frac exp
-
int
-
digit
digit1-9 digits
-
digit
-
digit1-9 digits
-
frac
-
.
digits
-
exp
-
e
digits
-
digits
-
digit
digits
digit
-
e
-
e
e+
e-
E
E+
E-
JSON建構於兩種結構:
- “名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),雜湊表(hash table),有鍵列表(keyed list),或者關聯陣列 (associative array)。
- 值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)。
這些都是常見的資料結構。事實上大部分現代電腦語言都以某種形式支援它們。這使得一種資料格式在同樣基於這些結構的程式設計語言之間交換成為可能。 JSON具有以下這些形式:
- 對象是一個無序的“‘名稱/值’對”集合。一個對象以“{”(左括弧)開始,“}”(右括弧)結束。每個“名稱”後跟一個“:”(冒號);“‘名稱/值’ 對”之間使用“,”(逗號)分隔。
- 數組是值(value)的有序集合。一個數組以“[”(左中括弧)開始,“]”(右中括弧)結束。值之間使用“,”(逗號)分隔。
- 值(value)可以是雙引號括起來的字串(string)、數值(number)、 ture、false、 null、對象(object)或者數組(array)。這些結構可以嵌套。
- 字串(string)是由雙引號包圍的任意數量Unicode字元的集合,使用反斜線轉義。一個字元(character)即一個單獨的字串(character string)。
JavaScript is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. It is widely believed to be a subset of Java, but it is not. It is a Scheme-like language with C-like syntax and soft objects. JavaScript was standardized in the ECMAScript Language Specification, Third Edition. JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss. var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] };In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members. Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
To convert a JSON text into an object, use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure.
var myObject = eval('(' + myJSONtext + ')');
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted. This is commonly the case in web applications when a web server is providing both the base page and the JSON data. There are cases where the source is not trusted. In particular, clients should never be trusted. When security is a concern it is better to use a JSON parser. A JSON parser will only recognize JSON text and so is much safer:
var myObject = myJSONtext.parseJSON();
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier. var myJSONText = myObject.toJSONString(); Demo:使用XML表示:
使用JSON: {comments:[ { id:1, author:"someone1", url:"http://someone1.x2design.net", content:"hello" }, { id:2, author:"someone2", url:"http://someone2.x2design.net", content:"hello" }, { id:3, author:"someone3", url:"http://someone3.x2design.net", content:"hello" } ]};
|