这是我的JSON数据数组。我想从名为UserID的JSON数组访问jsonResult字段。请为上述JSON建议正确的解决方案。

[
  {
    "UserRelationshipID":1,
    "SubordinateUserID":1014,
    "UserID":3,
    "UserRelationshipTypeID":1
  }
]

最佳答案

假设您具有对象数组,则可以获取用户ID,如下所示。以后,您可以根据需要将结果保存到变量或其他内容中,而不用安慰结果。



var jsonResult = [ {"UserRelationshipID":1,"SubordinateUserID":1014,"UserID":3,"UserRelationshipTyp‌​eID":1}, {"UserRelationshipID":1,"SubordinateUserID":2537,"UserID":3,"UserRelationshipTyp‌​eID":1}, {"UserRelationshipID":1,"SubordinateUserID":3,"UserID":4,"UserRelationshipTypeID‌​":1}, {"UserRelationshipID":1,"SubordinateUserID":7,"UserID":4,"UserRelationshipTypeID‌​":1}, {"UserRelationshipID":1,"SubordinateUserID":6,"UserID":2537,"UserRelationshipTyp‌​eID":1}, {"UserRelationshipID":1,"SubordinateUserID":3,"UserID":2537,"UserRelationshipTyp‌​eID":1}]

for(var i=0; i<jsonResult.length; i++){
  console.log(jsonResult[i].UserID)
}

关于javascript - 当我访问JSON数组中的特定值时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42782094/

10-14 08:37