gpt4 book ai didi

javascript - JSON 获取嵌套数组中的用户 ID

转载 作者:行者123 更新时间:2023-11-27 22:59:08 25 4
gpt4 key购买 nike

如何获取嵌套数组 JSON 中的所有用户 ID,我使用以下代码来获取 JSON 响应:

var userListObj = JSON.stringify(result.items);
console.log(userListObj);

如有任何帮助,我们将不胜感激,提前谢谢您。

{  
"current_page":1,
"per_page":100,
"total_entries":3,
"items":[
{
"user":{
"id":12729498,
"owner_id":51603,
"full_name":"test1",
"email":"test1",
"login":"test1",
"phone":null,
"website":null,
"created_at":"2016-05-16T14:56:24Z",
"updated_at":"2016-05-17T18:23:35Z",
"last_request_at":"2016-05-17T19:23:37Z",
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
},
{
"user":{
"id":12768158,
"owner_id":51603,
"full_name":"test2",
"email":"",
"login":"test2",
"phone":null,
"website":null,
"created_at":"2016-05-17T18:25:13Z",
"updated_at":"2016-05-17T18:25:13Z",
"last_request_at":null,
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
},
{
"user":{
"id":12769692,
"owner_id":51603,
"full_name":"test3",
"email":null,
"login":"test3",
"phone":null,
"website":null,
"created_at":"2016-05-17T19:22:55Z",
"updated_at":"2016-05-17T19:22:55Z",
"last_request_at":null,
"external_user_id":null,
"facebook_id":null,
"twitter_id":null,
"blob_id":null,
"custom_data":null,
"twitter_digits_id":null,
"user_tags":null
}
}
]
}

最佳答案

如果您应用@Mike C 的评论,您会得到如下内容:

var formattedJson = JSON.parse(json);

for (i=0;i<formattedJson.items.length;i++){
console.log(formattedJson.items[i].user.id);
}

formattedJson 取决于您获取的 JSON 是字符串还是有效的 JSON,如果是,则不需要使用 JSON.parse,只需访问它即可。

关于javascript - JSON 获取嵌套数组中的用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37286861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com