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