如何在不同的 javascript 数组中保存 JSON API 响应
How do I Save JSON API response in different javascript arrays
正在处理 API 通过 API 从 10K 英尺系统获取用户数据的响应。响应如下所示:
"data": [{
"id": 30000,
"display_name": "John Doe",
"email": "Johndoes@testmail.com",
},
我想将此数据保存在三个不同的数组中,以便稍后用于循环访问用户 ID 的每个响应。我不知道该怎么做。以下是我迄今为止使用的代码。我将不胜感激这里的任何帮助。谢谢
function getUsers() {
var auth = 'authentication'
var url = 'https://api.10000ft.com/api/v1/users?' + '&auth=' + auth;
var options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + auth
}
};
};
let response = UrlFetchApp.fetch(url, options);
let json = JSON.parse(response);
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response);
var ids = [];
var display_names = [];
var emails = [];
function result(data) {
data.forEach(element => {
ids.push(element.id);
display_names.push(element.display_name);
emails.push(element.email);
});
return { "ids": ids, "display-names": display_names, "emails": emails };
}
console.log(result(data));
据我了解...也许您正在寻找类似的东西?
let json = JSON.parse(response);
let ids = [];
let dis_name = [];
let email = [];
for(let i = 0; i < json.length; i++){
ids.push(json[i].id);
dis_name.push(json[i].display_name);
email.push(json[i].email);
}
P.S 考虑使用 let 关键字而不是 var.
如果 "data" 包含更多对象,您需要循环遍历数据数组并将 ID、display_names 和电子邮件推送到单独的数组中。下面的例子会帮助你。
var data = [
{
"id": 30000,
"display_name": "John Doe",
"email": "Johndoes@testmail.com"
},
{
"id": 30001,
"display_name": "John Cena",
"email": "JohnCens@testmail.com"
},
{
"id": 30002,
"display_name": "John kabaraya",
"email": "Johnkabarayas@testmail.com"
}
]
var ids = [];
var display_names = [];
var emails = [];
function result(data) {
data.forEach(element => {
ids.push(element.id);
display_names.push(element.display_name);
emails.push(element.email);
});
return { "ids": ids, "display-names": display_names, "emails": emails };
}
console.log(result(data));
正在处理 API 通过 API 从 10K 英尺系统获取用户数据的响应。响应如下所示:
"data": [{
"id": 30000,
"display_name": "John Doe",
"email": "Johndoes@testmail.com",
},
我想将此数据保存在三个不同的数组中,以便稍后用于循环访问用户 ID 的每个响应。我不知道该怎么做。以下是我迄今为止使用的代码。我将不胜感激这里的任何帮助。谢谢
function getUsers() {
var auth = 'authentication'
var url = 'https://api.10000ft.com/api/v1/users?' + '&auth=' + auth;
var options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + auth
}
};
};
let response = UrlFetchApp.fetch(url, options);
let json = JSON.parse(response);
};
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response);
var ids = [];
var display_names = [];
var emails = [];
function result(data) {
data.forEach(element => {
ids.push(element.id);
display_names.push(element.display_name);
emails.push(element.email);
});
return { "ids": ids, "display-names": display_names, "emails": emails };
}
console.log(result(data));
据我了解...也许您正在寻找类似的东西?
let json = JSON.parse(response);
let ids = [];
let dis_name = [];
let email = [];
for(let i = 0; i < json.length; i++){
ids.push(json[i].id);
dis_name.push(json[i].display_name);
email.push(json[i].email);
}
P.S 考虑使用 let 关键字而不是 var.
如果 "data" 包含更多对象,您需要循环遍历数据数组并将 ID、display_names 和电子邮件推送到单独的数组中。下面的例子会帮助你。
var data = [
{
"id": 30000,
"display_name": "John Doe",
"email": "Johndoes@testmail.com"
},
{
"id": 30001,
"display_name": "John Cena",
"email": "JohnCens@testmail.com"
},
{
"id": 30002,
"display_name": "John kabaraya",
"email": "Johnkabarayas@testmail.com"
}
]
var ids = [];
var display_names = [];
var emails = [];
function result(data) {
data.forEach(element => {
ids.push(element.id);
display_names.push(element.display_name);
emails.push(element.email);
});
return { "ids": ids, "display-names": display_names, "emails": emails };
}
console.log(result(data));