Basic JSON knowledge

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (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. Easy for reading and writing, and easy for machine parsing and generation.

 

 

Infrastructure

JSON is constructed in two structures:

 

1. A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).

 

2. 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.

 

Basic example

Simply put[1], JSON can convert a set of data represented in a javascript object to a string, and then it can be easily passed between functions, alternatively, the string is transmitted from the Web Client to the server in an asynchronous application. This string looks a bit odd, but javascript can easily explain it, and JSON can represent a more complex structure than a name/value pair. For example, it can represent arrays and complex objects, not just a simple list of keys and values.

JSON representation of name/value pairs

In the simplest form, you can use the following JSON to represent name/value pairs:

 

{"Firstname": "Brett "}

 

This example is very basic and actually takes more space than the equivalent plain text name/Value Pair:

 

Firstname = Brett

 

However, when multiple name/value pairs are concatenated, JSON will reflect its value. First, you can create records that contain multiple name/value pairs, for example:

 

{"Firstname": "Brett", "lastname": "McLaughlin", "email": "aaaa "}

 

In terms of syntax, this is not a great advantage over name/value pairs, but JSON is easier to use and more readable in this case. For example, it explicitly indicates that the above three values are part of the same record; curly braces make these values have a certain relationship.

Array

To represent a group of values, JSON not only improves readability, but also reduces complexity. For example, assume that you want to list a person's name. In XML, many start and end tags are required. If a typical name/value pair is used (like the name/value pair seen in the previous articles in this series ), you must either create a proprietary data format or change the key name to a person1-firstName.

 

If JSON is used, you only need to group multiple records with curly braces:

 

{"People ":[

 

{"Firstname": "Brett", "lastname": "McLaughlin", "email": "aaaa "},

 

{"Firstname": "Jason", "lastname": "Hunter", "email": "BBBB "},

 

{"Firstname": "elliotte", "lastname": "Harold", "email": "CCCC "}

 

]}

 

This is not hard to understand. In this example, there is only one variable named people. The value is an array containing three entries, each of which is a one-person record, including the name, last name, and email address. The preceding example demonstrates how to use parentheses to combine records into a value. Of course, you can use the same syntax to represent multiple values (each value contains multiple records ):

 

{"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": "Science Fiction "},

 

{"Firstname": "tad", "lastname": "Williams", "genre": "Fantasy "},

 

{"Firstname": "Frank", "lastname": "Peretti", "genre": "Christian fiction "}

 

],

 

"Musicians ":[

 

{"Firstname": "Eric", "lastname": "Clapton", "instrument": "Guitar "},

 

{"Firstname": "Sergei", "lastname": "Rachmaninoff", "instrument": "Piano "}

 

]}

 

The most noteworthy here is that it can represent multiple values, and each value contains multiple values. However, you should also note that the actual name/value pairs in the record can be different between different primary entries (programmers, authors, and musicians. JSON is completely dynamic and allows you to change the data representation mode in the middle of the JSON structure.

 

When processing data in JSON format, there are no predefined constraints to be observed. Therefore, in the same data structure, you can change the way data is represented, or even express the same thing in different ways.

 

Concept comparison

  Comparison between JSON and XML

 

Readability

 

The readability of JSON and XML is comparable. One side is a simple syntax and the other is a standard tag form, making it difficult to distinguish between the two.

 

Scalability

 

XML is inherently highly scalable, and JSON is also available. Nothing is XML, but JSON is not. However, JSON can store JavaScript composite objects at the home site of JavaScript, which has an incomparable advantage of XML.

 

Encoding difficulty

 

XML has rich coding tools, such as dom4j and JDOM, and JSON also provides tools. Without tools, I believe that skilled developers can quickly write the desired XML document and JSON string. However, the XML document requires many more structural characters.

 

Decoding difficulty

 

XML can be parsed in two ways:

 

One is to parse the document model, that is, index a group of tags through the parent tag. For example, xmldata. getelementsbytagname ("tagname"), but this method is used when the document structure is known in advance and cannot be encapsulated.

 

Another method is to traverse nodes (document and childnodes ). This can be achieved through recursion, but the parsed data is still in different forms and often cannot meet the pre-requirements.

 

It is difficult to parse such extensible structure data.

 

The same is true for JSON. If you know the JSON structure in advance, it is amazing to use JSON for data transmission. You can write code that is very practical, beautiful, and readable. If you are a pure front-end developer, you will love JSON very much. However, if you are an application developer, you are not that fond of it. After all, XML is the real structured markup language for data transmission.

 

However, parsing JSON without knowing the JSON structure would be a nightmare. Code will become redundant, and the results will be unsatisfactory. However, this does not affect the choice of JSON by many front-end developers. Because tojsonstring () in JSON. js can see the JSON string structure. Of course, this string is not used, so it is still a dream. After a common JSON user sees this string, the JSON structure is very clear, making it easier to operate JSON.

 

The preceding section only parses the XML and JSON data transmitted in JavaScript. In the Javascript field, JSON is a battle at home after all, and its advantage is of course far superior to XML. If JavaScript composite objects are stored in JSON and do not know their structure, I believe many programmers are crying to parse JSON.

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.