0

I am getting the below json return from wiki:

{
"query": {
    "pages": {
        "1514": {
            "pageid": 1514,
            "ns": 0,
            "title": "exampletitle",
            "extract": "Example extract"
        }
    }
}
}

How can I access the "extract" value from this json? I will not know the page id (1514) and can't simply do a json.query.pages.1514.extract;

(anyway .1514 will not work)

Thank you!

rtexal
  • 476
  • 6
  • 8
  • if you don't know page number, how do you suppose to get data in case there are 20 pages in `pages` object? Or you need ALL of them and looking for a way to iterate it as array? – Tommi Jul 11 '13 at 13:54

3 Answers3

2

You can iterate over the pages object to access its members

for(var key in json.query.pages){
    if(json.query.pages.hasOwnProperty(key)){
        console.log(json.query.pages[key].extract);
    }
 }
0

json.query.pages['1514'].extract should work

shyam
  • 9,134
  • 4
  • 29
  • 44
0

use JSON.parse()

ex:

var [your_parsed_JSON] = JSON.parse([your_JSON_variable])


heres a similar stackoverflow question with more detail/examples:

Parse JSON in JavaScript?


EDIT: actually it appears you are trying to access the contents of a container that you do not know. That's like trying to find the "lost treasure" without knowing where it is.

Reorganize your data structure.

Community
  • 1
  • 1
zigo
  • 159
  • 1
  • 10