views:

178

answers:

1

I use this code to gathering the information from Wiki:

http://en.wikipedia.org/w/api.php?action=query&rvprop=content&prop=revisions&format=json&titles=apple

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.

+1  A: 

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

Pascal MARTIN
can I get the "Something....." directly? One more thing, Assume, I don't know the "18978754".... Because this number it depends on different topic from wiki
Ted Wong
Hi, I've edited my answer to show an example of iterating through the pages, when you don't know their ids ; Hope this help :-)
Pascal MARTIN
You are very helpful, thx a lot~~~ It is really helpful.
Ted Wong
You're welcome :-) Have fun !
Pascal MARTIN
Can I follow the question? How can I get the "Something....." value? (Assume I don't know "Something....")both data.query.pages[pageId].revisions.* anddata.query.pages[pageId].revisions[*]are not working
Ted Wong
I've edited my answer a second time, to provide a full example (I supposed you'd find out how to get there yourself ; that's why I didn't go farther the first time) ; hope this helps :-)
Pascal MARTIN