1. The older method:
function strToJson(str) {
var json = eval('(' str ')');
return json;
}
2. More commonly used methods:
function strToJson(str){
return (new Function("return " str ))();
}
3. JSON object method not supported by IE67:
function strToJson(str){
return JSON.parse(str);
}
4. jQuery provides Method:
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(/"[^"\nr]*"|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 );
}
},
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn