The basic format of JSON and the conversion to JavaScript

Source: Internet
Author: User

The basic format of JSON and the conversion to JavaScript

Recently, many people have found that when writing JSON-formatted data, it is not always possible to achieve the JSON specification, although it can be passed without the language requirements being strict.

However, inevitably will encounter such a problem, the time to make changes, worried about the late, so plan to write this blog post.

Syntax rules for JSON:

1. Data needs to be written in the form of key-value pairs;

E G:

{   "name": "Zychao"  }

2, data and data need to be separated by commas;

E G:

{  "name": "Zychao",  "sex": "Male",  "age":

3. The data object needs to be covered by a pair of curly braces;

4, the ordered list of values;

E G: In the following case, the object users is an array, and each object is a user record in an array of key values.

{  "users" : [    {"firstName": "He", "LastName": "Yongqing"},    {"firstName": "Ding", "LastName": "Yingjie"},    {"fitstname": "Lucene", "LastName": "Zhang"}]}

JSON text converted to JavaScript object


We can parse the JSON string literal through the Json.parse () function and convert it to a JavaScript object.

var text = "{' users ': [" +       "{' FirstName ': ' He ', ' lastName ': ' Yongqing '}," +      "{' FirstName ': ' Ding ', ' lastName ' : ' Yingjie '} "+"]} ";
var obj = json.parse (text);

Finally, we can apply the JavaScript object to the written page.

The basic format of JSON and the conversion to JavaScript

Related Article

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.