ECMA-262 (E3) did not write the JSON concept to the standard, fortunately the concept of JSON in ECMA-262 (E5) was formally introduced, including the global JSON object and the Tojson method of date.
1,eval way to parse, I am afraid this is the earliest way to parse. As follows:
The code is as follows:
function var json = eval (' (' + str + ') ' return '
Remember to do not forget the parentheses on both sides of Str.
2,new function form, more strange oh. As follows
The code is as follows:
function Strtojson (str) { var json = (new Function ("return" + str));
3, use the global JSON object, as follows:
The code is as follows:
function return json.parse (str);}
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
Use Json.parse strict adherence to the JSON specification, such as attributes are required in quotation marks, as follows
The code is as follows:
var str = ' {name: ' Jack '} 'var//
The name is not enclosed in quotation marks, and the parsing fails with the exception thrown in all browsers using Json.parse. And the first two ways are fine.
Additional: Special implementations of Json.parse in Chrome
IE8/FIREFOX3.5+/CHROME4/SAFARI4/OPERA10 has implemented this method. The way to use it is simple:
The code is as follows:
var str = ' {' name ': ' Jack '} 'var json =
"Jack" will pop up in the browser that implements the method above.
If you add a method to Object.prototype to parse the JSON (someone might be strongly opposed to doing so polluting the native object, this is purely for discussion)
The code is as follows:
function () { return json.parse ( this
Because all objects inherit the method of object, this can be used directly,
The code is as follows:
var str = ' {' name ': ' Jack '} 'var json =
Str.parsejson (), this in Parsejson points to Str. Not all browsers will be able to parse successfully.
Ie8/firefox/safari/opera will still pop up "Jack", with the error in Chrome: uncaught illegal access.
Why does chrome not support this? Comparing two ways, the parameter passed to Json.parse is the string str, and the one is this. Looks like these two are no different?
When Str.parsejson (), Parsejson internal this point is supposed to be str. Modify the following Parsejson method:
The code is as follows:
function () { alert (typeofthis return json.parse ( this
Re-execute, you can find that Parsejson pops up is an object, perhaps this is the difference. You can see the obvious effect by simply new a string.
The code is as follows:
var js = json.parse (new String (' {' name ': ' Jack '} '
The above code except for Chrome error, other browsers are performing normal.
Basic conclusion:
In Chrome, the first argument to Json.parse can only be a string, not an object (including new string mode)
And then go back to the Object.prototype add a method to parse JSON, if you want to be compatible with all browsers, you can write:
The code is as follows:
function () { return json.parse (thisvar str = ' {' name ': ' Jack '} '
var json =
Transferred from: http://www.jb51.net/article/25988.htm
(RPM) JSON to object in JavaScript