Using Wiki API in JSON format cannot get content

I use this code to collect information from the 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 evaluate it to JSON, but the problem is that I can get into requests> pages, after which I can’t get a deeper understanding, because the Wiki API returns me as String 18978754, but it can’t get the value like this:

jsonObject.query.pages.18978754

Some assumption that I need to clarify, I do not know the number 18978754. Do I need to get the number first, or can I still get "Something ..." within the knowledge of the number.

+3
3

:

jsonObject.query.pages[18978754]

, , firebug:

>>> data.query.pages[18978754]
Object pageid=18978754 ns=0 title=Apple revisions=[1]

:

>>> data.query.pages[18978754].title
"Apple"

; :

>>> data['query'].pages[18978754].title
"Apple"

JS : -)


/

, , - :

for (var pageId in data.query.pages) {
    if (data.query.pages.hasOwnProperty(pageId)) {
        console.log(data.query.pages[pageId].title);
    }
}

, hasOwnProperty, , , , , - :

, hasOwnProperty. ; .

, "revision", , , , btw...


, : -)



:

, ( , ):

data.query.pages[pageId].revisions

- ( []), , , . , :

data.query.pages[pageId].revisions[0]

:

data.query.pages[pageId].revisions[1]

( , , btw - ^^)

.


, - , :

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

, , '*' :

data.query.pages[pageId].revisions[i]['*']


, :

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]['*']);
        }
    }
}

firebug, , :

Something.....


, :

for (var pageId in data.query.pages) {
    if (data.query.pages.hasOwnProperty(pageId)) {
        console.log(data.query.pages[pageId].revisions[0]['*']);
    }
}

, revisions.


: ; , , ; , , ; -)

+4

, - .

&indexpageids , pageids. .

+3

I had the same problem when creating a php parser for the Wiki API - this is how I solved it.

$new_result = array_pop($result["query"]["pages"]);

$final_result = $new_result["pageid"];

Note. I know this is the PHP code in the JS question, but could not find exactly the same question / problem in this forum - I am sure it will be useful for someone.

+1
source

Source: https://habr.com/ru/post/1714124/


All Articles