JSON rookie understanding

Source: Internet
Author: User

JSON's rookie understanding

JSON is a lightweight format for data interchange. JSON takes a completely language-independent text format, which makes JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate. JSON is used to convert between two languages, and JavaScript does not require any tools to process the JSON.

Function

1 collection of "name/value" pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), recording (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array). When JSON represents an object, use {Key:value,key1:value,key2:value} where Key1 represents the property of the object, and value represents the value of the property, called by the object. Key1 to get the corresponding value.

2. Ordered list of values (an ordered list of values). In most languages, it is understood as an array (array). obtained by index, its content can be a number or a string or a single character.

Advantages

JSON can convert the JS object into a set of strings, then you can call each other between functions, you can implement the content of the Web page and background processing between the transfer, JSON can simply represent an array. When you need to represent a set of values, JSON not only improves readability, but also reduces complexity. For example, suppose you want to represent a list of people. In XML, many start and end tags are required, and if you use a typical name/value pair (like the name/value pair you see in the previous article in this series), you must create a proprietary data format or change the key name to a form such as Person1-firstname. Baidu

If you use JSON, you only need to group multiple records with curly braces together:

{"People": [

{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}

]}

Then the people variable contains an array of three entries, each of which can be obtained by index, and each entry is a person's record, which contains first name, last name, and e-mail address.

For example, you can create a new JavaScript variable, and then assign the JSON-formatted data string directly to it:

var people = {"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"}

] }

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;

Rewind string

Of course, if the object cannot be easily converted back to the text format mentioned in this article, then all data modifications are not of much value. This conversion is also simple in JavaScript:

String Newjsontext = people.tojsonstring ();

The final conclusion is that JSON is almost certainly a good choice if you are dealing with a large number of JavaScript objects, so you can easily convert the data into a format that can be sent to the server-side program in the request.

JSON rookie understanding

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.