简体   繁体   English

从JSON多维数组获取值?

[英]Getting values from JSON multidimensional array?

I have a JSON array that looks like this: 我有一个看起来像这样的JSON数组:

{
    "id": 258,
    "rawId": null,
    "displayName": null,
    "name": {
        "givenName": "my ame",
        "honorificSuffix": "",
        "formatted": "my ame",
        "middleName": "",
        "familyName": "",
        "honorificPrefix": ""
    },
    "nickname": "",
    "phoneNumbers": [{
        "value": "23423442342424",
        "pref": false,
        "id": 0,
        "type": "mobile"
    }],
    "emails": null,
    "addresses": null,
    "ims": null,
    "organizations": [{
        "pref": "false",
        "title": "",
        "name": "",
        "department": "",
        "type": null
    }],
    "birthday": null,
    "note": "",
    "photos": null,
    "categories": null,
    "urls": null
}

I need to get the phoneNumbers >> value from this JSON. 我需要从此JSON获取phoneNumbers >>值。

SO I TRIED SOMETHING LIKE THIS: 所以我尝试了一些类似的事情:

var d = JSON.parse(test); alert(test[0].phoneNumbers.value);

The variable test is the JSON shown above. 变量test是上面显示的JSON。

and I also tried: 而且我还尝试了:

alert(d[0].phoneNumbers.value);

and

alert(test.phoneNumbers.value);

But none of the above work. 但以上工作均无效。

Is there something that I am missing in my code? 我的代码中缺少什么吗?

Thanks in advance. 提前致谢。

What you showed us is a JSON string (giving a JS object after parsing), not an array. 您展示给我们的是一个JSON字符串(解析后提供一个JS对象),而不是一个数组。

So d[0].phoneNumbers will not work and d.phoneNumbers will work and will give you an array. 因此d[0].phoneNumbers将不起作用,而d.phoneNumbers将起作用并为您提供一个数组。

And because it will give you an array, d.phoneNumbers.value will not work, and d.phoneNumbers[0].value will. 并且因为它将为您提供一个数组, d.phoneNumbers.value将不起作用,而d.phoneNumbers[0].valued.phoneNumbers[0].value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM