When using the JSON.parse(data) method of the JSON2.JS file, I encountered a problem:
throw new SyntaxError('JSON.parse');
When querying information, the general meaning is as follows:
When the JSON.parse method encounters an unparsable string, it will throw a SyntaxError exception.
That is: JSON.parse(text, reviver), This method parses a JSON text to produce an object or array. t can throw a SyntaxError exception.
So, here is a summary of the mutual conversion of objects and strings in JS, because this involves browser compatibility issues.
1: The conversion method supported by jQuery plug-in
The code is as follows:
$.parseJSON(jsonstr); //jQuery.parseJSON(jsonstr), you can convert json strings into json objects
In turn, use serialize series methods: such as: var fields = $("select, :radio").serializeArray();
2: Browser supported conversion methods (Firefox, chrome, opera, safari, ie9, ie8) and other browsers
The code is as follows:
JSON.parse(jsonstr); //You can convert json strings into json objects
JSON.stringify(jsonobj); //You can convert json objects into json pair strings
Note: ie8 (compatibility mode), ie7 and ie6 do not have JSON objects, and json.js or json2.js need to be introduced.
3: Javascript supports conversion methods
eval('(' + jsonstr + ')'); //You can convert json strings into json objects, note that you need to wrap a pair of brackets outside the json character
Note: ie8 (compatibility mode), ie7 and ie6 can also use eval() to convert strings into JSON objects, but these methods are not recommended. This method is not safe for eval to execute expressions in the json string.
The above detailed explanation of the mutual conversion of objects and strings in JS is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.