Javascript 从维基百科 API 中提取数据 [英] Javascript extract data from Wikipedia API

查看:37
本文介绍了Javascript 从维基百科 API 中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从维基百科 API(http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&callback=revisions&rvprop=content&callback=?)

我能够从维基百科获取数据,但无法从中提取数据.

function getJSONP(url, success) {var ud = '_' + +新日期,script = document.createElement('script'),head = document.getElementsByTagName('head')[0] ||文档.documentElement;窗口[ud] = 函数(数据){head.removeChild(脚本);成功&&成功(数据);};script.src = url.replace('callback=?', 'callback=' + ud);head.appendChild(脚本);}getJSONP('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?', function(data){控制台日志(数据);document.getElementById("output").innerHTML = data.query;});

想在这里显示文章标题.

下面是它在控制台中显示的内容.如何提取来自:List_of_metropolitan_areas_by_population""并在前端显示?

解决方案

您试图将 HTML 元素的内容变成一个 javascript 对象,这就是为什么您会看到您所看到的结果.您想要的是字符串,因此您需要访问将给您字符串的对象部分.

document.getElementById("output").innerHTML = data.query.normalized[0].from;

代替

document.getElementById("output").innerHTML = data.query;

这是与您的答案一起的小提琴链接:http://jsfiddle.net/zsaj950t/

How do I extract Title (List of metropolitan areas by population) from WIkipedia API(http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?)

I was able to get data from Wikipedia but having trouble extract data from it.

function getJSONP(url, success) {
    var ud = '_' + +new Date,
        script = document.createElement('script'),
        head = document.getElementsByTagName('head')[0] || document.documentElement;

    window[ud] = function(data) {
        head.removeChild(script);
        success && success(data);
    };

    script.src = url.replace('callback=?', 'callback=' + ud);
    head.appendChild(script);

}
getJSONP('http://en.wikipedia.org/w/api.php?format=json&action=query&titles=List_of_metropolitan_areas_by_population&prop=revisions&rvprop=content&callback=?', function(data){
    console.log(data);
    document.getElementById("output").innerHTML = data.query;
}); 

<div id="output">
    Want to display article title here.
</div>

below is what it shows up in Console. How can I extract "from: "List_of_metropolitan_areas_by_population"" and display in front-end?

解决方案

You were trying to make the content of an HTML element a javascript object, which is why you saw the result you saw. What you want is the string, so you need to access the part of the object that will give you the string.

document.getElementById("output").innerHTML = data.query.normalized[0].from;

Instead of

document.getElementById("output").innerHTML = data.query;

Here's a link to the fiddle to go along with your answer: http://jsfiddle.net/zsaj950t/

这篇关于Javascript 从维基百科 API 中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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