首页 新闻 搜索 专区 学院

大家帮忙看下这个json取值出错的问题(4颗豆,无法悬赏,抱歉)

0
[已关闭问题] 关闭于 2015-10-09 16:57

有如下json

var d={
    "msShops": [
        {
            "shopId": 1000002418,
            "address": null,
            "agentId": 0,
            "comments": null,
            "companyId": "1000002417",
            "invalidDate": 4593859200000,
            "isPost": "1",
            "linkMan": "张二狗",
            "opMan": null,
            "phone": "0755-28419014",
            "registDate": 1330683908000,
            "shopName": "二狗红河店",
            "headimgurl": null,
            "smsNumber": 1,
            "sonCompanyId": "1000002417",
            "reserveendtime": 0,
            "reservestarttime": 0,
            "mmUserSms": null,
            "msCompany": {
                "companyId": 1000002417,
                "address": null,
                "companyCode": "二狗造型时尚店",
                "companyName": "二狗造型时尚店",
                "companyType": "1",
                "linkMan": "张二狗",
                "openAccount": "Y",
                "parentId": "0",
                "phone": "13570850825",
                "status": "1"
            },
            "wxPhotos": null
        },
        {
            "shopId": 2001106074,
            "address": "广东 深圳",
            "agentId": null,
            "comments": null,
            "companyId": "2001106073",
            "invalidDate": 1432816801000,
            "isPost": "0",
            "linkMan": "王店长",
            "opMan": null,
            "phone": "15818515787",
            "registDate": 1377664321000,
            "shopName": "路易士",
            "headimgurl": null,
            "smsNumber": 962,
            "sonCompanyId": "2001106073",
            "reserveendtime": 0,
            "reservestarttime": 0,
            "mmUserSms": null,
            "msCompany": {
                "companyId": 2001106073,
                "address": "广东 深圳",
                "companyCode": "路易士",
                "companyName": "路易士",
                "companyType": "1",
                "linkMan": "王店长",
                "openAccount": "Y",
                "parentId": "0",
                "phone": "15818515787",
                "status": "0"
            },
            "wxPhotos": null
        }
    ],
    "meta": {
        "rescode": 1,
        "msg": null,
        "url": null,
        "res1": null,
        "res2": null
    }
}

我使用JS来取值(jquery),就报错了,尽管在chrome控制台能拿到值,但报错阻止了代码继续运行。

for(var k in d.msShops){
  console.log(d.msShops[k].shopId);
}

报错如下:

酸番茄的主页 酸番茄 | 初学一级 | 园豆:87
提问于:2015-10-09 16:53
< >
分享
所有回答(1)
0

你先试着

console.log(d.msShops[k]);

好像数据变化的,不推荐用for(var k in array)的写法

五毛钱的饼 | 园豆:317 (菜鸟二级) | 2015-10-09 16:57

for(var k in d.msShops){ console.log(d.msShops[k].shopId); }

改成下面的试试输出的结果

var lengths = d.msShops.length;

for(var i=0;i<lengths;i++){

  var tempShop = d.msShops[i];

  console.log(tempShop);

  console.log(tempShop.shopId);

}

支持(0) 反对(0) 五毛钱的饼 | 园豆:317 (菜鸟二级) | 2015-10-09 17:00

@wakasann: 感谢回复,我刚取出来试了 又没问题 但放在环境中的JS就出问题··

支持(0) 反对(0) 酸番茄 | 园豆:87 (初学一级) | 2015-10-09 17:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册