简体   繁体   English

解析JSON以获取JavaScript中对象内部数组中的值

[英]Parsing JSON to get values in array inside a object in javascript

I am newbie to JSON and Javascript So,help me to find the array values in an object in JSON.Here I hava a JSON Object like this 我是JSON和Javascript的新手,所以,请帮助我在JSON对象中查找数组值。在这里,我有一个像这样的JSON对象

{
"DefinitionSource": "",
"ImageWidth": 0,
"RelatedTopics": [
{
    "Result": "<a href=\"https://duckduckgo.com/Ashok_Shinde\">Ashok Shinde</a>An Indian film and stage actor, who works in Marathi cinema, known for films like Rangat Sangat.",
    "Icon": {
        "URL": "",
        "Height": "",
        "Width": ""
    },
    "FirstURL": "https://duckduckgo.com/Ashok_Shinde",
    "Text": "Ashok ShindeAn Indian film and stage actor, who works in Marathi cinema, known for films like Rangat Sangat."
},
{
    "Result": "<a href=\"https://duckduckgo.com/R._Ashok\">R. Ashok</a>R. Ashok is a leader of the Bharatiya Janata Party in Karnataka, India.",
    "Icon": {
        "URL": "",
        "Height": "",
        "Width": ""
    },
    "FirstURL": "https://duckduckgo.com/R._Ashok",
    "Text": "R. AshokR. Ashok is a leader of the Bharatiya Janata Party in Karnataka, India."
},
{
    "Result": "<a href=\"https://duckduckgo.com/Ashok_(film)\">Ashok (film)</a>",
    "Icon": {
        "URL": "https://duckduckgo.com/i/37704bcf.jpg",
        "Height": "",
        "Width": ""
    },
    "FirstURL": "https://duckduckgo.com/Ashok_(film)",
    "Text": "Ashok (film)A 2006 Telugu film directed by Surender Reddy."
}
],
"Entity": "",
"Results": [],
"Answer": ""
}

In this JSON i want to get "URL" inside the ICON object and "TEXT" from each object inside "RelatedTopics".. But i was unable to find how. 在此JSON中,我想在ICON对象中获取“ URL”,并从“ RelatedTopics”内的每个对象中获取“ TEXT”。。但是我找不到方法。 Please help to Get out of this problem. 请帮助摆脱这个问题。 Thanks in advance 提前致谢

You have to loop the array of objects and use the index to check the properties of each: 您必须循环对象数组并使用索引检查每个对象的属性:

for (var i = 0; i < data.RelatedTopics.length; i++) {
    console.log(data.RelatedTopics[i].Text); //text;
    console.log(data.RelatedTopics[i].Icon.URL) //url;
}

您也可以尝试

results.map(function(e){ return e.Icon.URL; });

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

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