如何在嵌套数组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
}
}
]
}

答案 0 :(得分:1)
如果您应用@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
,您只需访问它。
答案 1 :(得分:0)
像这样:
var json = {
"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
}
}
]
};
var arr = [];
json.items.forEach(function(item) {
arr.push(item.user.id);
});
console.log(arr);
答案 2 :(得分:0)
这是我的解决方案,评论和错误检查:
var x = '{"current_page":1, "per_page":100, "total_entries":3, "items":[{"user":{"id":12729498}},{"user":{"id":12768158}},{"user":{"id":12769692}}]}';
// Parse the JSON
var userListObj = JSON.parse(x);
// Check if any entries exist
if (typeof userListObj.items != 'undefined' && userListObj.items.length > 0)
{
// Loop through each user
for (var i in userListObj.items)
{
// Check if a user ID exists
if (typeof userListObj.items[i].user.id != 'undefined')
{
// Output the user ID to the console
console.log (userListObj.items[i].user.id);
}
}
}