JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript programming language, standard ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
-
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-character T-
"
-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 is constructed in two structures:
- A collection of name/value pairs ). In different languages, it is understoodObject), Record, structure (struct), Dictionary, hash table, keyed list, or associative array ).
- An ordered list of values ). In most languages, it is understood as an array ).
These are common data structures. In fact, most modern computer languages support them in some form. This makes it possible to exchange a data format between programming languages that are also based on these structures. JSON has the following forms:
- An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.
- An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.
- Value (Value) Can be a string enclosed by double quotation marks (String), Number, true, false, null, object, or array ). These structures can be nested.
- String (StringIs a set of any number of Unicode characters enclosed by double quotation marks, which are escaped using a backslash. A character (character) is a separate string (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, useEval ()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 + ')');
Theeval Function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The useeval 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 particle, clients shoshould 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 ction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give your iCal structures to the JSON stringifier. var myJSONText = myObject.toJSONString(); Demo: in XML format:
Use 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" } ]}; |