如何在Javascript中从URL获取JSON? [英] How to get JSON from URL in Javascript?
本文介绍了如何在Javascript中从URL获取JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此URL 返回JSON:
{
query: {
count: 1,
created: "2015-12-09T17:12:09Z",
lang: "en-US",
diagnostics: {},
...
}
}
我试过这个,但它不起作用:
I tried this, and it didn't work:
responseObj = readJsonFromUrl('http://query.yahooapis.com/v1/publ...');
var count = responseObj.query.count;
console.log(count) // should be 1
怎么能我从这个URL的JSON响应中获取了一个JavaScript对象?
How can I get a JavaScript object from this URL's JSON response?
推荐答案
你可以使用jQuery .getJSON()
功能:
You can use jQuery .getJSON()
function:
$.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(data) {
//data is the JSON string
});
如果您不想使用jQuery,那么您应该看看纯JS解决方案的答案:< a href =https://stackoverflow.com/a/2499647/1361042> https://stackoverflow.com/a/2499647/1361042
If you don't want to use jQuery you should look at this answer for pure JS solution: https://stackoverflow.com/a/2499647/1361042
这篇关于如何在Javascript中从URL获取JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文