Home > Article > Backend Development > Three ways to convert strings to json format in js
Don’t forget the parentheses on both sides of str. Method two, new Function form
Method 3, use global JSON object
Currently in IE8(S)/Firefox3.5+ /Chrome4/Safari4/Opera10 has implemented this method. When using JSON.parse, you must strictly abide by the JSON specification. For example, attributes need to be enclosed in quotation marks, as follows: var str = '{name:"jack"}'; var obj = JSON.parse(str); // --> parse error The name is not enclosed in quotes. When using JSON.parse, an exception is thrown in all browsers and the parsing fails. The first two methods are fine. |