Javascript - 解析长 GET 请求 JSON

标签 javascript json parsing

我正在通过 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]); 但返回错误

最佳答案

使用JSON.parse

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/

相关文章:

javascript - 为什么我们在 JS 中使用类似 $(function() { }) 的语法?

javascript - 如何以最少的步骤验证数组是否存在及其长度?

javascript - Nodejs 应用程序抛出不能使用 body-parser 中间件发布/用户

javascript - 数据不显示

android - 尝试使用 okhttpClient 连接到 URL 以获取 Json

java - 无法处理 SOAP 响应

algorithm - 递归下降优先级解析缺失前缀表达式

javascript - 获取大数的最后 13 位数字

javascript - 我可以避免在 ng-repeat 循环中使用对象变量名吗?

html - 如何转义 CSS 中的单引号或双引号?