我正在通过 JS 向 https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=API-KEY-HERE 发送 GET 请求并且它有效。虽然,我完全不知道如何解析弹出的长 JSON。我想获取第一篇文章的标题。
代码:
var HttpClient = function () {
this.get = function (aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function () {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open("GET", aUrl, true);
anHttpRequest.send(null);
}
}
var client = new HttpClient();
client.get('https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=08f3b70e722d46ebab1fdd5b5499f671', function (response) {
console.log(response);
});
编辑:我尝试使用 console.log(response['articles'][0]);
但返回错误
最佳答案
var client = new HttpClient();
client.get('https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=<your-api-key-here>', function (response) {
var json = JSON.parse(response);
console.log(json['articles'][0]);
});
关于Javascript - 解析长 GET 请求 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44478425/