Skip to content Skip to sidebar Skip to footer

How To Access Nested Object In Json Returned By Wikipedia Api

From this query to the Wikipedia API: http://en.wikipedia.org/w/api.php?action=query&prop=links&format=json&plnamespace=0& pllimit=10&titles=List%20of%20televis

Solution 1:

Yes, you will need to loop over it. See also the very similiar question parse json string from wikimedia using jquery. You could also receive a list of result pageids using the indexpageids parameter.

if (data && data.query && data.query.pages)
    var pages = data.query.pages;
else// error: No pages returned / other problems!for (var id in pages) { // in your case a loop over one propertyvar links = pages[id].links
    if (links)
        for (i=0; i<links.length; i++) {
            // do what you want with links[i]
        }
    else// error: No links array returned for whatever reasons!
}

Solution 2:

This should get you there...

var links, i, pageID;

for (pageID in query.pages) {
    links = query.pages[pageID].links;
    for (i=0; i<links.length; i++) {
        link = links[i];
        link.ns; link.title // handle link obj here.
    }
}

Post a Comment for "How To Access Nested Object In Json Returned By Wikipedia Api"