13. 字符串转为json对象或json数组

Posted on 2017-06-01 15:36  zkx4213  阅读(608)  评论(0编辑  收藏  举报

##########1.json字符串转json数组###########

var str="[{name:'zhangsan',age:'24'},{name:'lisi',age:'30'},{name:'wangwu',age:'16'},{name:'tianqi',age:'7'}] ";  

var myobj=eval(str);  

//var myobj= eval('(' + str + ')');

for(var i=0;i<myobj.length;i++){  

  alert(myobj[i].name);  

   alert(myobj[i].age);  

 

##########2.json字符串转json对象###########

JSON字符串:
var str1 = '{ "name": "cxh", "sex": "man" }';
JSON对象:
var str2 = { "name": "cxh", "sex": "man" };

//由JSON字符串转换为JSON对象

var obj = eval('(' + str1 + ')');

然后,就可以这样读取:

Alert(obj.name);

Alert(obj.sex);

Copyright © 2024 zkx4213
Powered by .NET 9.0 on Kubernetes