Three ways to parse JSON data in JavaScript __java

Source: Internet
Author: User
Tags tojson

Overview
The JSON format is now becoming more and more important in web development, especially in the process of using AJAX development projects, which often involves returning JSON-formatted strings to the front end and parsing the front end into a JS object (JSON).
ECMA-262 (E3) did not write the JSON concept into the standard, but fortunately in ECMA-262 (E5) The concept of JSON was formally introduced, including global JSON objects and date Tojson methods.
Three ways to parse JSON data
Eval () method
The most common way to parse JSON data is to use the eval () method of JavaScript, which is as follows:

function Tojson (str) {
 var json = eval (' + str + ') ');
 return JSON;
}

This method has performance and security problems and is not recommended for use.
New Function method

function Tojson (str) {
 var json = (new Function ("return" + str)) ();
 return JSON;
}

Json.parse () method
This method only supports IE8/FIREFOX3.5+/CHROME4/SAFARI4/OPERA10 versions, these browsers are close to the standard of the web, and the Tojson method is implemented by default.

function Tojson (str) {return
 json.parse (str);
}

Json2.js will use the native version when the browser natively supports Json.parse, and it is compatible with the ES5 API. In the ES5 is not fully popularized in the current situation, John Resig great God recommended Json2.js mainly for now can be used with the ES5 compatible API, in the future can smooth transition to es5--as long as the removal of an import to change over.

Original address:http://www.jb51.net/article/68842.htm

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.