使用js读取json字符串内容的方法

时间:2021-05-02

json技术使用比较广泛,下面介绍几种使用js读取json字符串内容的方法:

var json = { contry:{ area:{ man:"12万", women:"10万" } } };//方式一:使用eval解析 var obj = eval(json); alert(obj.constructor);alert(obj.contry.area.women);

//方式二:使用Funtion函数 var strJSON = "{name:'json name'}";//得到的JSON var obj = new Function("return" + strJSON)();//转换后的JSON对象 alert(obj.name);//json name alert(obj.constructor);//复杂一点的json数组数据的解析 var value1 = [{"c01":"1","c02":"2","c03":"3","c04":"4","c05":"5","c06":"6","c07":"7","c08":"8","c09":"9"}, {"c01":"2","c02":"4","c03":"5","c04":"2","c05":"8","c06":"11","c07":"21","c08":"1","c09":"12"}, {"c01":"5","c02":"1","c03":"4","c04":"11","c05":"9","c06":"8","c07":"1","c08":"8","c09":"2"}]; var obj1 = eval(value1); alert(obj1[0].c01);

//复杂一点的json的另一种形式 var value2 = {"list":[ {"password":"1230","username":"coolcooldool"}, {"password":"thisis2","username":"okokok"}], "array":[{"password":"1230","username":"coolcooldool"},{"password":"thisis2","username":"okokok"}]};

var obj2 = eval(value2); alert(obj2.list[0].password);

本文转自百度知道

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章