JSON string converted to JSON object

Source: Internet
Author: User
Tags string to json

JSON string converted to JSON object
var str = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
1. Use the eval () method
The eval () function uses the JavaScript compiler to parse the JSON text and then generate the JavaScript object. You must enclose the text in parentheses in order to avoid grammatical errors.
var obj = eval (' (' + str + ') '); Convert from JSON string to JSON object
document.getElementById ("FName"). Innerhtml=obj.name
2. Using the Json.parse () method
var obj = json.parse (str); Convert from JSON string to JSON object
3. Using the $.parsejson () method
var obj = $.parsejson (str); Convert from JSON string to JSON object
4. The official JSON conversion method:
http://www.json.org/provides a json.js so that IE8 (compatibility mode), IE7 and IE6 can support JSON objects as well as their stringify () and Parse () methods;
Can get this JS on Https://github.com/douglascrockford/JSON-js, generally now use json2.js.
Then, you can read this:
Alert (Obj.name);
Alert (Obj.sex);

JSON string converted to JSON object

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.