JSON (JavaScript Object Notation (Mark , Mark))is a lightweight data interchange format. It is based onJavaScript(Standard ECMA-262 3rd edition-december 1999) is a subset of the. JSONtext format that is completely language-independent, but also uses a similarCthe habits of the language family (includingC, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features enableJSONbecome the ideal data exchange language. Jsoneasy to read and write, but also easy to machine parse and generate.
First, Comparison of JSON and XML
1. Readability
JSON and XML are comparable in readability, with simple syntax on one side and a canonical label on the other, which makes it difficult to distinguish between the winners and losers.
2. Scalability
XML is inherently well-extensible,and JSON is of course, but json is at home in Javascript and can store Javascript Compound object, has The incomparable advantage of XML.
3, Coding difficulty
XML has a wealth of coding tools , such as dom4j,JDom , and so on, andJSON also provides tools. Without tools, it is believed that a skilled developer can quickly write the desired XML document and JSON character string, butthe XML The document has many more characters on the structure.
4. Decoding difficulty
There are two ways to parse XML:
one is to parse through the document model, that is, to index a set of tags through the parent tag. For example:xmldata.getelementsbytagname ("TagName"), but this is to be used in the case of pre-knowledge of the document structure, unable to do a generic encapsulation. Another way is to traverse nodes (document and childNodes). This can be achieved by recursion , but the parsed data is still in different forms and often does not meet the requirements of the pre-order.
It must be difficult to parse any such extensible structure data.
JsonThe same is true. If you know beforehandJSONstructure of the case, use theJSONdata transfer is simply fantastic, and you can write code that is useful and visually readable. If you are purely a front-desk developer, you will be very fond ofJSON. But if you're an app developer, you're not so fond of it, after allXMLis really structured.Markup Language, which is used for data passing. And if you don't knowJsonthe structure of the solutionJSON, it's a nightmare. Time and effort not to say, the code will become redundant procrastination, the results are also unsatisfactory. But this does not affect the choice of many foreground developersJSON. Becausejson.jsin thetojsonstring ()you'll seeJSONthe string structure. Of course not using this string, it's still a nightmare. CommonJSONafter seeing the string, the personJSONThe structure is very clear, it is easier to operateJSON.
the above is inJavascriptonly for data passing in theXMLwith theJSONthe resolution. In theJavascriptsite,JSONafter all, it is home combat, and its advantages are of course far superior toXML. IfJSONstored inJavascriptcompound object, and do not know its structure, I believe many programmers are also crying parsingJSONthe.
5. Effective data Rate
In addition to the above, Another big difference between JSON and XML is the effective data rate. JSON is more efficient when it is transmitted as a packet format because JSON does not need to have strict closing tags like XML, which increases the amount of effective data to the total packet ratio. Thus reducing the transmission pressure of the network in the case of equivalent data traffic .
Second, data formats supported by JSON
There are two basic constructs of JSON construction:
1,A collection of name/value pairs
2,an ordered list of values. In the most languages, the realized as an array, vector, list, or sequence.
JSON is simply called objects and arrays in JavaScript, so these two structures are objects and arrays of two structures that can represent a variety of complex structures .
1, objects: Objects inJSis represented in"{}"The data structure is the content of the{Key:Value,key:Value,...}The structure of the key-value pairs, in the object-oriented language,Keyis the property of the object,valueto the corresponding attribute value, so it is easy to understand that the value method is the object. KeyGets the property value, the type of the property value can be a number, a string, an array, several objects. The object is an unordered"'name/value'the"collection.
(1) An object starts with "{"(opening parenthesis) and"}"(closing parenthesis) ends.
(2) each " name " followed by a ":"(colon);
(3) use of"' name / value ' pair " "," (comma) delimited.
Example 1:{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"}
2, array: The array in JS is the bracketed "[]" in the content, data structure for ["java", "JavaScript", "VB" ,...] , the value is obtained in the same way as in all languages, and the type of the field value can be a number, a string, an array, and several objects. An array isanordered collection of values (value).
(1) An array begins with "["(the left square bracket), and"]"(the right square bracket) ends.
(2) use ","(comma) to separate values.
through the object, the array 2 kinds of structure can be combined into a complex data structure.
Example 2: contains three people objects, and these three objects are placed in an array.
{
"People": [
{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},
{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},
{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}
]
}
Example 3: Contains three programmers, three authors, and two musicians, each of which is placed in an array.
{"Programmers": [
{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},
{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},
{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}
],
"Authors": [
{"FirstName": "Isaac", "LastName": "Asimov", "Genre": "Sciencefiction"},
{"FirstName": "Tad", "LastName": "Williams", "Genre": "Fantasy"},
{"FirstName": "Frank", "LastName": "Peretti", "Genre": "Christianfiction"}
],
"Musicians": [
{"FirstName": "Eric", "LastName": "Clapton", "instrument": "Guitar"},
{"FirstName": "Sergei", "LastName": "Rachmaninoff", "Instrument": "Piano"}
]}
JSON basics, Simple introduction