JS operation JSON Summary

Source: Internet
Author: User

JSON strings are parsed into JSON data formats in JS. There are two methods:

1. Use the eval_r () function.

2. Use the Function object for return parsing.

In the data transmission process, json is transmitted in the form of text, that is, strings, while JS operates on JSON objects. Therefore, the conversion between JSON objects and JSON strings is the key. For example:

JSON string:
Var str1 = '{"name": "cxh", "sex": "man "}';

JSON object:
Var str2 = {"name": "cxh", "sex": "man "};

Solution 1:
Var dataObj = eval_r ("(" + data + ")"); // convert to a json object
Why do you want to add "(" ("+ data +") "); //" to eval?

The reason is: Problems with eval itself. Json starts and ends in the form of "{}". In JS, json is treated as a statement block. Therefore, it must be forcibly converted into an expression.

The purpose of parentheses is to force the eval function to forcibly convert the expressions in parentheses into objects when processing JavaScript code, rather than executing them as statements. For example, if no outer brackets are added to the object literal {}, eval identifies the braces as the start and end mark of the JavaScript code block, then {} is considered to have executed an empty statement. The following two execution results are different:
Alert (eval_r ("{}"); // return undefined
Alert (eval_r ("({})"); // return object [Object]

For this method, we can see it everywhere in JS.

For example, (function () {} (); During the closure operation.

Var str1 = '{"name": "cxh", "sex": "man "}';
Var data = eval_r ("(" + str1 + ")"); // convert to a json object // data = (new
Alert (data. name); // cxh is displayed.

Note that the eval_r () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems. Therefore, you can use third-party client script libraries that circumvent eval_r (). For example, JSON in JavaScript provides a script library of no more than 3 kb.

Solution 2:
The second method of parsing is to use the Function object. Its typical application is to parse the data returned by success under the AJAX method in JQUERY.
Var str1 = '{"name": "cxh", "sex": "man "}';
Var data = (Function ("", "return" + str1 ))();
Alert (data. name); // cxh is displayed.

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.