On the processing _javascript of JSON data in JS

Source: Internet
Author: User

1. JSON data structure (object and array)

JSON object: var obj = {"Name": "Xiao", "Age": 12};

JSON array: var objarray = [{' Name ': ' Xiao ', ' age ': 12},{' name ': ' Xiao ', ' Age ': 12}];

2, processing JSON data, dependent files are: jquery.js

3. Note: In the process of data transmission, JSON data is in the form of text, that is, string format;

JS language is the operation of JS objects;

So the conversion between JSON string and JS object is the key;

4. Data format

JSON string: var json_str = ' {' name ': ' Xiao ', ' Age ': 12} ';

Josn object: var obj = {"Name": "Xiao", "Age": 12};

JS Object: Object = {name: "Xiao", age:12}

5. Type conversion

JSON string-->js object, using the method:

Indicate:

Json_str and obj represent the data types in sub Heading 4 of this article;

obj = Json.parse (JSON_STR);

obj = Jquery.parsejson (JSON_STR);

Note: Incoming malformed JSON string (for example: ' {name: ' Xiao ', age:12} ') throws an exception;

JSON string format, strict format: ' {' name ': ' Xiao ', ' Age ': 12} '

JS Object-->json String:

Json_str = JSON. Stringify (obj);

Note:

1, eval () is a JS native function, using the form: eval (' + ' {name: "Xiao", Age:12} ') '), is not safe, can not guarantee the conversion of the type to JS object;

2, the above 3 methods, have passed the Chrome browser test, the following is a screenshot of the test results;

JSON string-->js object;

JS Object-->json String:

To Garden Friends:

For any errors in the text, you are welcome to point out.

The above discussion of JS JSON data processing is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.