JSON strings and JSON objects convert to each other

Source: Internet
Author: User
Tags string to json

During data transfer, JSON is passed in the form of text, which is a string, and JS operates on JSON objects, so the conversion between JSON objects and JSON strings is key

For example:

JSON string: var str = ' {' name ': ' name1 ', ' sex ': ' m '} ';

JSON object: var obj = {"Name": "Name1", "Sex": "W"};

One, JSON string converted to JSON object

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

var obj = eval (' (' + str + ') '); //From JSON string to JSON object, the text must be enclosed in parentheses in order to avoid syntax errors: "(" + str+ ")"

or var obj = $.parsejson (str); Convert a JSON string to a JSON object (jQuery)

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

This can be read:

Console.log (Obj.name);

Console.log (Obj.sex);

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

Second, the JSON object can be converted to a JSON string using tojsonstring () or global Method Json.stringify ().

For example: Var str=obj.tojsonstring (); Convert a JSON object to a JSON character

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

Summarize:

As we can see from the above, there are generally two types of conversions, one is the parser that comes with JavaScript, and the other is the JSON parser. Where the JavaScript parser can compile and execute any JavaScript code so there is a potential security problem hidden and the JSON parser can only recognize JSON text, not compile the script so it is more secure, and the JSON parser is faster.

In the above several methods, in addition to the eval () function is JS own, the other several methods are from the Json.js package. The new version of JSON modifies the API to inject json.stringify () and Json.parse () two methods into JavaScript's built-in objects, the former becoming object.tojsonstring (), And the latter becomes String.parsejson (). If you are prompted not to find the tojsonstring () and Parsejson () methods, your JSON package version is too low.

JSON strings and JSON objects convert to each other

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.