js读取json里面的数据多种方式

var json = {  contry:{ area:{ man:"12万",  women:"10万" } } };
//方式一:使用eval解析  
    var obj = eval(json);  // 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值