What about using array-syntax :
jsonObject.query.pages[18978754]
Seems to be working, using firebug :
>>> data.query.pages[18978754]
Object pageid=18978754 ns=0 title=Apple revisions=[1]
And :
>>> data.query.pages[18978754].title
"Apple"
Note accessing data-object with an array-syntax is also possible for the other properties ; for instance :
>>> data['query'].pages[18978754].title
"Apple"
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 :
for (var pageId in data.query.pages) {
if (data.query.pages.hasOwnProperty(pageId)) {
console.log(data.query.pages[pageId].title);
}
}
Note that I'm using hasOwnProperty
to 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 :
Every object descended from Object
inherits the hasOwnProperty method.
This method can be used to determine
whether an object has the specified
property as a direct property of that
object; unlike the in operator, this
method does not check down the
object's prototype chain.
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) :
data.query.pages[pageId].revisions
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 :
data.query.pages[pageId].revisions[0]
The second one this way :
data.query.pages[pageId].revisions[1]
(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 :
var num_revisions = data.query.pages[pageId].revisions.length;
var i;
for (i=0 ; i<num_revisions ; i++) {
console.log(data.query.pages[pageId].revisions[i]);
}
And now, inside that loop, you should be able to get the '*' property of the given object :
data.query.pages[pageId].revisions[i]['*']
So, the final code becomes :
for (var pageId in data.query.pages) {
if (data.query.pages.hasOwnProperty(pageId)) {
var num_revisions = data.query.pages[pageId].revisions.length;
var i;
for (i=0 ; i<num_revisions ; i++) {
console.log(data.query.pages[pageId].revisions[i]['*']);
}
}
}
Using this code in firebug, I now get the literral sting you're looking for :
Something.....
Of course, you could probably just use :
for (var pageId in data.query.pages) {
if (data.query.pages.hasOwnProperty(pageId)) {
console.log(data.query.pages[pageId].revisions[0]['*']);
}
}
Which will work fine if you always want to deal with only the first element of the revisions
array.
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 ;-)