通过JSON格式使用wiki API无法获取内容 [英] Using Wiki API by format JSON can't get the content

查看:220
本文介绍了通过JSON格式使用wiki API无法获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code从维基收集信息:

I use this code to gathering the information from Wiki:

<一个href=\"http://en.wikipedia.org/w/api.php?action=query&rvprop=content&prop=revisions&format=json&titles=apple\" rel=\"nofollow\">http://en.wikipedia.org/w/api.php?action=query&rvprop=content&prop=revisions&format=json&titles=apple

我可以得到一个JSON字符串像这样

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....."
                }]
            }
        }
    }
}

我可以给的eval JSON,但问题是,我可以进入查询>页面,在那之后我不能得到更深层次的,这是因为维基API返回我作为一个字符串18978754,但它可以 ŧ该得到的值:

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

一些假设我要澄清,我不知道电话号码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 :

jsonObject.query.pages[18978754]

似乎是工作,使用Firebug:

Seems to be working, using firebug :

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

>>> 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"

这是完全有效的JS语法: - )

That's perfectly valid JS syntax :-)


添加seing评论/编辑后

如果您不知道页面的ID,你可以在页面迭代,像这样的东西:

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

请注意,我使用的 的hasOwnProperty 以确保我的对象具有属性,它没有任何种类的继承或任何像来临即:

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 :

每一个对象从Object的后裔
  继承的hasOwnProperty方法。
  这种方法可以被用来确定
  对象是否已经指定
  财产的一个直接财产
  目的;不像在运营商,这
  方法不检查下来
  对象的原型链。

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...

结果
希望这有助于更好地: - )

结果


第二个编辑,之后第二组的意见:

好了,去更远(没想到你是认真的字面意思):

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]

(没有第二个你提供的,顺便说一句的例子 - 所以这是在理论上^^)

等。

结果
要获得这些对象的每个人,你必须做一些类型的循环,像这样的:


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

结果
所以,最终code变为:


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

在使用本萤火code,我现在给你要找的literral刺:

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

这将正常工作,如果你总是希望处理仅修订的第一个元素阵列。

结果
只是要注意:在你的榜样,只有一个版本;在code我提供的应该能够处理许多;由你来决定你想要的那些做; - )


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

这篇关于通过JSON格式使用wiki API无法获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆