I use this code to gathering the information from Wiki:
And I can get a JSON String like this
{
"query": {
"normalized": [{
"from": "apple",
"to": "Apple"
}],
"pages": {
"18978754": {
"pageid": 18978754,
"ns": 0,
"title": "Apple",
"revisions": [{
"*": "Something....."
}]
}
}
}
}
I can eval it to JSON, but the problem is, I can get into the query>pages, after that I can’t get deeper, it was Because the Wiki API return me as a String 18978754, but it can’t get the value by this:
jsonObject.query.pages.18978754
Some assumption I need to clarify, I don’t know the number 18978754. Do I need to get the number first or I can still get “Something…” within knowing the number.
What about using array-syntax :
Seems to be working, using firebug :
And :
Note accessing data-object with an array-syntax is also possible for the other properties ; for instance :
That’s perfectly valid JS syntax 🙂
Added after seing the comment / edit
If you don’t know the ids of the pages, you can iterate over the pages, with something like this :
Note that I’m using
hasOwnPropertyto be sure the object I’m on has the property, and that it’s not coming from any kind of inheritance or anything like that :Depending on what’s in “
revision“, you might have to do the same on that one too, btw…Hope this helps better 🙂
Second edit, after second set of comments :
Well, going a bit farther (didn’t think you meant it literally) :
is an array (note the
[]symbols) that seems to be able to contain several objects.so, you can get the first one of those this way :
The second one this way :
(there is no second one in the example you provided, btw — so this is in theory ^^ )
And so on.
To get everyone of those objects, you’d have to do some kind of loop, like this :
And now, inside that loop, you should be able to get the ‘*’ property of the given object :
So, the final code becomes :
Using this code in firebug, I now get the literral sting you’re looking for :
Of course, you could probably just use :
Which will work fine if you always want to deal with only the first element of the
revisionsarray.Just beware : in your example, there was only one revision ; the code I provided should be able to deal with many ; up to you to determine what you want to do with those 😉