【问题标题】:Parsing properties of unknown JSON object解析未知 JSON 对象的属性
【发布时间】:2013-11-24 03:04:51
【问题描述】:

我正在尝试使用 MediaWiki API 从 Wikipedia 获取文章。

但是,返回的 JSON 数据包含 pageid 作为 Object 的标识符,但 pageid 是随机的。

我的代码如下所示:

$.getJSON("http://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=extracts&exchars=50000&format=json&callback=?", function (data) {
    console.log(data.query.pages);
});

返回的第一页的 JSON 是这样的:

37889571: Object
    extract: "<p></p>↵<p></p>↵<p><b>Frank A Delaney IV</b>, Born Feb 4, 1963, Denville New Jersey.</p>↵<p>Mr Delaney is the son of Frank A Delaney III, ..."
    ns: 0
    pageid: 37889571
    title: "Frank A Delaney IV"

我想获取诸如摘录或标题console.log(data.query.pages.37889571.extract); 之类的属性,但由于 pageid 是随机的,我不确定如何才能获得它。有什么好的方法可以做到这一点?

对于那些感兴趣的人,这里是 JSON:

{

    "query":{
        "pages":{
            "1302977":{
                "pageid":1302977,
                "ns":0,
                "title":"Russ O'Hara",
                "extract":"<p><b>Russell Eugene Nealeigh O'Hara</b> is a one-time Los Angeles, USA radio personality whose career began at the forefront of the Boss Radio/Top 40 format.</p>\n<p>O'Hara is best known among LA radio listeners for his work on KKDJ<sup class=\"plainlinks noprint Inline-Template\" style=\"vertical-align:text-top;white-space:nowrap;\">[<i>disambiguation needed</i>]</sup> and his longtime solo work on KRLA where he went by the occasional moniker of \"Russ O'Hungry.\" Highly visible among Los Angeles radio personalities, O'Hara had the privilege of introducing headliners at major concerts, including Janis Joplin at the Hollywood Bowl; he also introduced The Rolling Stones, Jimi Hendrix and Three Dog Night among many others.</p>\n<p>He remains active as a broadcaster and currently hosts afternoons on KDES-FM, an oldies station in Palm Springs, California.</p>\n<p>O'Hara worked at the following Los Angeles stations in the course of his career:</p>\n<ul><li>(KBLA), 1964-65.</li>\n<li>KGFJ, 1968-60.</li>\n<li>KRLA, 1969-72.</li>\n<li>KKDJ<sup class=\"plainlinks noprint Inline-Template\" style=\"vertical-align:text-top;white-space:nowrap;\">[<i>disambiguation needed</i>]</sup>, 1972-74.</li>\n<li>KEZY (Anaheim) 1975-77.</li>\n<li>KROQ-FM, 1978-79.</li>\n<li>Return to KRLA, 1981\u201382; 1992-93.</li>\n<li>KEZN Palm Desert 1993-2001.</li>\n</ul><h2>External links</h2>\n<ul><li>KDES webpage</li>\n</ul><p><br></p>"
            }
        }
    }

}

【问题讨论】:

  • 当我阅读您对 JSON 的(相当糟糕的)再现时,您有一个包含更多 JSON 对象的 JSON 对象。其中一个包含的对象名为 37889571。该对象又包含一个名为“extract”的字符串。您始终可以遍历外部对象中的所有条目并获取所有“提取”字符串。但显然要获得一个特定的,您需要知道它的“名称”或其他标识符。
  • @HotLicks 感谢您的编辑
  • 每天都有几十个几乎相同的 JSON 问题需要一个新站点:jsonoverflow.com
  • 相关:This recent question 提出的问题基本相同,但针对的是 Python。

标签: json mediawiki-api


【解决方案1】:

我可以通过以下方式访问属性:

for(var id in data.query.pages) {
  console.log(data.query.pages[id]);
}

但我不确定这是否是最好/唯一的方法。

【讨论】:

  • 这是我推荐自己的方式。也就是说,如果您出于某种原因不想想要使用 for..in 循环,您可以也可以向 MediaWiki 询问 @987654321 @ 在响应的单独部分中。
【解决方案2】:

您现在可以在请求中添加&amp;indexpageids,您将获得pageids

例如:
https://en.wikipedia.org/w/api.php?action=query&titles=Leipzig&prop=pageimages&format=json&pithumbsize=1000&indexpageids

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多