简体   繁体   English

使用JavaScript和GET请求从api网址提取JSON数据的最佳方法是什么?

[英]What is the best way extract JSON data from an api url using javascript and a GET request?

I've hit a wall trying to figure out how to send a request to the BandsInTown API and be able to extract a few elements (datetime and venue latitude and longitude) from the returned JSON page that contains music event information. 我碰到了一堵墙,试图弄清楚如何向BandsInTown API发送请求,并能够从返回的包含音乐事件信息的JSON页面中提取一些元素(日期时间以及场地的纬度和经度)。 I'm pretty new to JavaScript and web development so any help is much appreciated! 我对JavaScript和Web开发还很陌生,因此非常感谢您的帮助! The URL I'm using as a request is: http://api.bandsintown.com/artists/Hippo%20Campus/events.json?lapi_version=2.0&app_id=music_matcher 我用作请求的URL是: http : //api.bandsintown.com/artists/Hippo%20Campus/events.json?lapi_version=2.0&app_id=music_matcher

where I will implement flexibility to change the artist called. 在这里我将实现灵活性,以改变这位艺术家。

I'm not sure if I should add the 'callback= something ' param in the url or not also. 我不确定是否应该在网址中添加'callback = something '参数。 I've looked into the 'requests' library through Node.js but I'm not totally sure how to get Node.js and npm to work through Eclipse Neon (the IDE I'm using) so I was really trying to just keep it straightforward and find a pure javascript solution that I could implement directly into my script in Eclipse. 我已经通过Node.js查看了“ requests”库,但是我不太确定如何让Node.js和npm通过Eclipse Neon(我正在使用的IDE)来工作,所以我真的试图保持它简单明了,并且找到了可以直接在Eclipse中的脚本中实现的纯JavaScript解决方案。 I just don't quite understand how to extract the JSON and call it later in my script as results[0].venue.latitude (in order to return latitude of venue) or whatever the 'results' object would be called (this is also part of my issue). 我只是不太了解如何提取JSON并稍后在脚本中将其作为results[0].venue.latitude (以返回场地的纬度)或任何称为“ results”的对象(这是也是我的问题的一部分)。 Do I even need to put this information into an object? 我什至需要将此信息放入对象中吗?

I ended up using JSON.parse() in addition to an async HTTP request like this: 除了像这样的异步HTTP请求外,我最终还使用了JSON.parse():

function httpGetAsync(theUrl, callback) { //theURL or a path to file
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        var data = JSON.parse(httpRequest.responseText);
        if (callback) {
            callback(data);
        }                   
    }
};

httpRequest.open('GET', theUrl, true); 
httpRequest.send(null);
}

and calling it: 并调用它:

httpGetAsync(url, function(data) {
document.write(data[0].venue.latitude);
document.write(data[0].venue.longitude);
document.write(data[0].datetime);
});

However, this method only allows me to make one clean call to the api. 但是,此方法仅允许我对api进行一次干净的调用。 When I try to make multiple calls using a loop that calls the function with different urls, my script seems to terminate after the first one. 当我尝试使用使用不同网址调用函数的循环进行多次调用时,我的脚本似乎在第一个脚本之后终止。 So this is still an issue for my application of this. 因此,这对于我的应用仍然是一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript从JSON API提取数据 - Extract data from JSON API using Javascript 使用模板库从ajax请求接收json数据后生成html代码的最佳方法是什么? - What is the best way to generate html code after receiving json data from an ajax request using a template library? 如何使用 JSON 从 Reddit API 中提取 url 数据 - How to extract url data from Reddit API using JSON 使用 Javascript 解析来自 Api GET 请求的 JSON 数据 - Parsing JSON data from Api GET request with Javascript 是否有一个在线工具可以从某个远程URL获取XML,然后提供一种使用javascript通过json获取数据的方法? - Is there an online tool that grabs XML from some remote URL then provides a way to get the data via json using javascript? 如何使用 jQuery 或纯 JavaScript 从 URL (REST API) 到 UI 获取 JSON 数据? - How to get JSON data from the URL (REST API) to UI using jQuery or plain JavaScript? 从JavaScript中的字符串中使用'='提取变量的最佳方法是什么 - what is the best way to extract variables with '=' from a string in javascript 在 vanilla JavaScript 中向服务器发送 GET 请求的最佳方式是什么? - What is the best way to send a GET request to the server in vanilla JavaScript? JavaScript从API网址加载JSON数据 - JavaScript load JSON data from an API url 使用Javascript或Ajax从HTML或JSP中的API URL获取数据 - Get data from a API URL in HTML or JSP using Javascript or Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM