Includes global JSON objects and date Tojson methods.
1,eval Way of parsing, I am afraid this is the earliest analytic way.
View sourceprint?1 function Strtojson (str) {
2 var json = eval (' (' + str + ') ');
3 return JSON;
4}
Remember the parenthesis on both sides of the Str.
2,new function form, more bizarre Oh.
View sourceprint?1 function Strtojson (str) {
2 var json = (new function ("return" + str)) ();
3 return JSON;
4}
3, use the global JSON object.
View sourceprint?1 function Strtojson (str) {
2 return Json.parse (str);
9 ·
Currently IE8 (s)/FIREFOX3.5+/CHROME4/SAFARI4/OPERA10 has implemented this method.
The use of json.parse requires strict adherence to the JSON specification, such as a property that is enclosed in quotes, as follows
View sourceprint?1 var str = ' {name: ' Jack '} ';
2 var obj = json.parse (str); --> Parse Error
Name is not enclosed in quotation marks, and parsing fails with the exception being thrown in all browsers using Json.parse. The first two ways are fine.