Home  >  Article  >  php教程  >  Summary of methods for converting json format strings into javascript objects or arrays

Summary of methods for converting json format strings into javascript objects or arrays

高洛峰
高洛峰Original
2016-12-08 09:45:311311browse

The first one

JSON.parse(jsonString)

The second one

eval("("+jsonString+")")

The third one

var obj=(function ToJSON(o){
 if(typeof(o)=="string")try{return new Function("return "+o)();}catch(e){return null;}
})("[{name:'zhangsan',age:'24'},{name:'lisi',age:'30'},{name:'wangwu',age:'16'},{name:'tianqi',age:'7'}]");
for(var b in obj)alert(obj[b].name);


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