MAK
MAK

Reputation: 390

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

{
"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. Please help to Get out of this problem. Thanks in advance

Upvotes: 0

Views: 1739

Answers (2)

Seth Malaki
Seth Malaki

Reputation: 4486

You can also try

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

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

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;
}

Upvotes: 2

Related Questions