Three ways to convert a string to JSON in JS

Source: Internet
Author: User
Tags string to json

1, the Eval method analysis, I am afraid this is the earliest analytic way. As follows:

functionvar json = eval (' (' + str + ') 'return '

2, new function form, more strange oh. As follows:

functionvar json = (new function ("return" +return

3. Use the global JSON object as follows:

functionreturn

At present IE8 (S)/FIREFOX3.5+/CHROME4/SAFARI4/OPERA10 has implemented the method, the following is part of the information: http://blogs.msdn.com/ie/archive/2008/09/10/ Native-json-in-ie8.aspx Https://developer.mozilla.org/en/Using_JSON_in_Firefox
The use of json.parse requires strict adherence to the JSON specification, as attributes are enclosed in quotation marks, as follows:

var str = ' {name: ' Jack '} 'var//

Originally from: http://www.jb51.net/article/25987.htm

  

Three ways to convert a string to JSON in JS

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.