Several methods of parsing string into json are summarized. For more information, see. 1. The old method:
The Code is as follows:
Function strToJson (str ){
Var json = eval ('+ str + ')');
Return json;
}
2. Common Methods:
The Code is as follows:
Function strToJson (str ){
Return (new Function ("return" + str ))();
}
3. json object methods not supported by IE67:
The Code is as follows:
Function strToJson (str ){
Return JSON. parse (str );
}
4. Methods provided by jQuery:
The 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 );
}
},