Simple understanding of JSON modules

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data interchange format. It makes it easy for people to read and write. It also facilitates the analysis and generation of machines. It is based on a subset of JavaScript programming Language, Standard ECMA-262 3rd edition-december 1999. JSON uses a text format that is completely independent of the program language, but also uses the C language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language.

JSON is based on two types of structures:

JSON[1] structures have two structures [2]json is simply the objects and arrays in JavaScript, so these two structures are objects and arrays of two structures that can be represented by a variety of complex structures
    • 1. Object: The object in JS is represented as "{}" in the content, the data structure is {Key:value,key:value,...} The structure of key-value pairs, in object-oriented languages, key is the property of the object, value is the corresponding property value, so it is easy to understand that the value method is the object. Key Gets the property value, the type of the property value can be a number, a string, an array, several objects.
    • 2, array: The array in JS is the middle bracket "[]" in the content, data structure for ["Java", "JavaScript", "VB",...], the value way and all languages, using index get, the type of field value can be number, string, array, object several.
{    "Animals": {        "Dog": [            {                "name":"Rufus",                " Age": 15            },            {                "name":"Marty",                " Age": null}]}

Through the object, the array 2 kinds of structure can be combined into a complex data structure.

A collection of name/value pairs (A collection of name/value pairs). In different programming languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative array). The sequence of values (an ordered list of values). In most languages, it is implemented as an array (array), vector (vectors), list, sequence (sequence).

These are common data structures. Currently, most programming languages support them in some form. This makes it possible to exchange data in the same format between various programming languages. The JSON has these forms:An Objectis an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma). An arrayis an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values. the value (value) can be a double-quoted string ( string ), numeric (),,,, number object, true false null or array. These structures can be nested. A Stringis a collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash. A character (character) is a separate string (character string). A string of JSON (String ) is very similar to a C or Java string. the value (number) is also very similar to the value of C or Java. Only JSON values are not used in octal and hexadecimal format. At the same time, you can add white space between any tags. accessing data

Although it doesn't seem obvious, the long string above is really just an array, and after you put the array into a JavaScript variable, it's easy to access. In fact, you simply represent the array element with a dot notation. So, to access the last name of the first entry in the programmers list, just use the following code in JavaScript:

people.programmers[0].lastName;
Note that the array index is zero-based. So, this line of code first accesses the data in the people variable, then moves to the entry called programmers, then moves to the first record ([0]), and finally accesses the value of the LastName key. The result is a string value of "McLaughlin". Here are a few examples of using the same variable.

people.authors[1].genre//Valueis"fantasy"
people.musicians[3].lastName//Undefined.Thisreferstothefourthentry,andthereisn‘tone
people.programmers[2].firstName//Valueis"Elliotte"

With this syntax, you can work with any JSON-formatted data without the need for any additional JavaScript toolkits or APIs.

Compare readability with XML

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.

Scalability

XML is inherently well-extensible, and JSON certainly does, and nothing is XML that can be extended and JSON is not extensible. However, JSON in the JavaScript home combat, can store JavaScript compound object, with XML incomparable advantage.

Coding Difficulty

XML has a wealth of coding tools, such as dom4j, Jdom, and so on, and JSON also provides tools. Without tools, it is believed that skilled developers can quickly write the desired XML document and JSON string, but the XML document has many more characters on the structure.

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 through recursion, but the parsed data is still different form, often can not meet the pre-requirements.

It must be difficult to parse any such extensible structure data.

The same is true for JSON. If you know the JSON structure in advance, it is wonderful to use JSON for data passing, and you can write very useful, aesthetically readable code. If you are a purely front-desk developer, you will love json very much. But if you're an app developer, it's not like that, after all, XML is the real structured markup language for data transfer.

It's a nightmare to parse JSON without knowing the structure of the JSON. Time and effort not to say, the code will become redundant procrastination, the results are also unsatisfactory. However, this does not affect many foreground developers to choose JSON. Because toJSONString () in json.js, you can see the string structure of the JSON. Of course not using this string, it's still a nightmare. When people who use JSON see the string, the JSON structure is straightforward and the JSON is easier to manipulate.

The above is the parsing of XML and JSON only for data passing in JavaScript. In JavaScript, JSON is a home battle, and its advantages are certainly far superior to XML. If you store JavaScript composite objects in JSON and don't know their structure, I'm sure a lot of programmers are crying and parsing json as well.

In addition to the above, there is 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 allows the amount of effective data to be significantly higher than the total packet ratio, thus reducing the transmission pressure of the network in the case of equivalent data traffic.

JSON Standard: Rfc4627:theApplication/json Media Type for JavaScript Object Notation (JSON)
ImportJsondata= Json.loads ('{"ID": "2", "IP": "12.12.12.12", "Port": " +"}')Printdata['ID'] Output results:"2"Data=json.dumps (data)PrintData Output result: {"ID":"2","IP":"12.12.12.12","Port":" the"}

Simple understanding of JSON modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.