The method of converting a JSON string into a JSON object by JS parsing _javascript techniques

Source: Internet
Author: User
Tags string to json

For example:

JSON string:
var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object:
var str2 = {"Name": "Cxh", "Sex": "Man"};

One, JSON string converted to JSON object

To use the above str1, you must first convert to a JSON object by using the following method:

Convert from JSON string to JSON object

var obj = eval (' (' + str + ') ');

Or

var obj = Str.parsejson (); Convert from JSON string to JSON object

Or

var obj = json.parse (str); Convert from JSON string to JSON object

Then, you can read this:

Alert (Obj.name);

Alert (Obj.sex);

Special Note: If obj is a JSON object, then using the eval () function to convert (even multiple conversions) is a JSON object, but there is a problem with using the Parsejson () function (throwing a syntax exception).

You can use the tojsonstring () or global method Json.stringify () to convert the JSON object to a JSON string.

For example:

var last=obj.tojsonstring (); Convert a JSON object to a JSON character

Or

var last=json.stringify (obj); Convert a JSON object to a JSON character

alert (last);

Attention:

In several of the above methods, except for the eval () function, which is JS-self-contained, several other methods come from the Json.js package. The new version of JSON modifies the API by injecting the json.stringify () and Json.parse () two methods into the Javascript built-in object, which becomes the object.tojsonstring (), which becomes the Strin G.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () methods, your JSON package version is too low.

PS: This site also provides several powerful JSON parsing, conversion and formatting tools for everyone to choose to use, I believe that the next JSON Format data processing will help:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

Online Xml/json Convert to each other:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

C language Style/html/css/json code formatting Landscaping tool:
Http://tools.jb51.net/code/ccode_html_css_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.