1. The old method:
Copy codeThe Code is as follows:
Function strToJson (str ){
Var json = eval ('+ str + ')');
Return json;
}
2. Common Methods:
Copy codeThe Code is as follows:
Function strToJson (str ){
Return (new Function ("return" + str ))();
}
3. json object methods not supported by IE67:
Copy codeThe Code is as follows:
Function strToJson (str ){
Return JSON. parse (str );
}
4. Methods provided by jQuery:
Copy codeThe Code is as follows:
ParseJSON: function (data ){
If (typeof data! = "String" |! Data ){
Return null;
}
Data = jQuery. trim (data );
If (/^ [\], :{}\ s] * $/. test (data. replace (/\\(? : ["\\\/Bfnrt] | u [0-9a-fA-F] {4})/g ,"@")
. Replace (/"[^" \ n \ r] * "| true | false | null | -? \ D + (? : \. \ D *)? (? : [EE] [+ \-]? \ D + )? /G, "]")
. Replace (/(? : ^ |: | ,)(? : \ S * \ [) +/g ,""))){
Return window. JSON & window. JSON. parse?
Window. JSON. parse (data ):
(New Function ("return" + data ))();
} Else {
JQuery. error ("Invalid JSON:" + data );
}
},