JSON to string, json to object

Source: Internet
Author: User

JSON to object official text:

Address: http://www.json.org/js.html

To convert a JSON text into an object and you can use the eval () function. eval () invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler would correctly parse the text and produce an object structure. The text must is wrapped in parens to avoid tripping on a ambiguity in JavaScript ' s syntax.

var myObject = eval (' (' + myjsontext + ') ');

The first way:

Use the JS function eval ();

Testjson=eval (Testjson); Is the wrong way to convert.

The correct conversion method needs to be added (): Testjson = eval ("(" + Testjson + ")");

Eval () is very fast, but he can compile and execute any JavaScript program, so there is a security issue. When using eval (). The source must be trustworthy. You need to use a more secure JSON parser. In the case where the server is not strictly encoded in JSON or if the input is not rigorously validated, it is possible to provide invalid JSON or a dangerous script, execute the script in eval (), and release the malicious code.

JS Code:
  function Converttojsonforjs () {            //var Testjson = "{name: ' Xiao Qiang ', age:16}";(support)            // var Testjson = "{' name ': ' Xiao Qiang ', ' age ': 16}"; (support)            var testjson = ' {' name ': "Xiao Qiang", "Age": +}'            ; // Testjson=eval (Testjson);//Wrong conversion mode            Testjson = eval ("(" + Testjson + ")");            alert (testjson.name);        }

The second method uses the Jquery.parsejson () method to format the JSON more highly, and must conform to the JSON format

Jquery.parsejson ()

JS Code:
  function converttojsonforjq () {            var testjson = ' {' name ': "Xiao Qiang", "Age": +}'            ; // Don't know            // ' {name: ' Xiao Qiang ', age:16} ' (name is not wrapped with double quotes)            // "{' Name ':" Xiao Qiang ", ' Age ': +}" (name using single quotes)            Testjson = $.parsejson (testjson);            alert (testjson.name);        }



JSON to string, json to 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.