简体   繁体   English

如何在 Javascript 中循环遍历 JSON 响应以获取特定数据

[英]How to loop through JSON response in Javascript to get specific data

I am learning how to make API requests using Javascript.我正在学习如何使用 Javascript 发出 API 请求。

I have made a GET request and got the following data and I would like to loop through them to get only id , display_name and email .我发出了 GET 请求并获得了以下数据,我想遍历它们以仅获取iddisplay_nameemail This are to be stored in arrays.这将存储在数组中。 How do I complete the javascript code?我如何完成javascript代码?

"data": [
        {
            "id": 222226,
            "display_name": "jane Doe",
            "email": "janeDoe@testmail.com",
        },
        {
            "id": 1111226,
            "display_name": "james",
            "email": "james@testmail.com",
        },
    {
            "id": 11126,
            "display_name": "Travis Mc",
            "email": "travis@testmail.com",
         },

Heres is my javascript code.这是我的 javascript 代码。

var options = {
        method: 'get',
        headers: {
            Authorization: 'Bearer ' + auth
        }
    };
    var response= UrlFetchApp.fetch(url, options);
    var data = JSON.parse(response.getContentText());

I am not sure I am doing this loop part right.我不确定我在做这个循环部分是否正确。

for (var i = 0; i < data.length; i++) {
    Logger.log(data[i].display_name);
}

在此处输入图片说明

I am going to assume you are using the Fetch Api .我将假设您正在使用Fetch Api If so this means you are creating a Promise.如果是这样,这意味着您正在创建一个 Promise。 When working with promises you will have to chain a then function to establish how to proceed.使用 promise 时,您必须链接then函数来确定如何继续。 Promise.then((value)=>{ /* do something */ }); Since you are working with JSON data you must first parse it before it can become manipulatable for javascript.由于您正在处理 JSON 数据,因此您必须先对其进行解析,然后才能使其可用于 javascript。 If you read the Fetch Api it explains how it resolves your promise and returns a Response based on the HTTP Response .如果你读了提取API,它说明了它是如何解决自己的诺言,并返回一个响应基于HTTP响应

This exampled is borrowed from the Devloper Mozilla Org Check out the usage section of the Fetch API这个例子是从 Devloper Mozilla Org 借来的查看 Fetch API 的使用部分

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });

This examples illustrates how the fetch function resolves the promise that returns a response which is parsed then returned again to another then function which will now have the parsed data ready to manipulate.此示例说明了 fetch 函数如何解析返回响应的承诺,该响应被解析然后再次返回到另一个then函数,该函数现在可以处理已解析的数据。

In your case the code will look something like this在你的情况下,代码看起来像这样

var options = {
    method: 'get',
    headers: {
        Authorization: 'Bearer ' + auth
    }
};

var response = UrlFetchApp.fetch(url, options)
.then((r)=>{ return r.json(); }).then((data)=>{
  for (let i = 0; i < data.length; i++) {
    Logger.log(data[i].display_name);
  }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM