在 javascript 中为多个 JSON 对象解析 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18877505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON in javascript for multiple JSON objects
提问by ace
Should be quite a common answer but I haven't found it.
应该是一个很常见的答案,但我还没有找到。
Using client-side javascript:
使用客户端javascript:
My client receives some JSON string:
我的客户端收到一些 JSON 字符串:
response =
[
{"id1":"value1" , "time1":"valuetime1"},
{"id2":"value2" , "time2":"valuetime2"}
]
I understand that I can simply parse the JSON string with this command:
我知道我可以使用以下命令简单地解析 JSON 字符串:
response = JSON.parse(response);
But what is the next command to access each of the objects individually? I would prefer to not use jQuery.
但是,单独访问每个对象的下一个命令是什么?我宁愿不使用 jQuery。
采纳答案by Eric Hotinger
回答by penguat
This JSON will become the appropriate JavaScript object. In this case, that is an arraywhich you can access the same way as you would any other JavaScript array:
这个 JSON 将成为合适的 JavaScript 对象。在这种情况下,这是一个数组,您可以像访问任何其他 JavaScript 数组一样访问它:
for (var i = 0; i < response.length; i++){
console.log(response[i]);
}