2014-11-14 92 views
0

我新手,JSON和Javascript所以,帮我找到在JSON.Here对象数组值我哈瓦一个JSON对象,这样JSON解析得到数组中值的对象中的JavaScript

{ 
"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": "" 
} 

在这个JSON中,我想要在ICON对象中获取“URL”,并从“RelatedTopics”中的每个对象中获取“TEXT”..但是我无法找到如何。请帮助摆脱这个问题。在此先感谢

回答

2

你要循环对象的数组和使用索引来检查每个属性:

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

谢谢......这是工作..感谢快速响应。 – MAK 2014-11-14 15:39:37

1

您也可以尝试

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