在JavaScript中解析JSON以获取多个JSON对象 [英] Parsing JSON in javascript for multiple JSON objects

查看:83
本文介绍了在JavaScript中解析JSON以获取多个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个很常见的答案,但我还没有找到.

Should be quite a common answer but I haven't found it.

使用客户端javascript:

Using client-side javascript:

我的客户端收到一些JSON字符串:

My client receives some JSON string:

response = 
[
 {"id1":"value1" , "time1":"valuetime1"},
 {"id2":"value2" , "time2":"valuetime2"}
]

我了解我可以使用以下命令简单地解析JSON字符串:

I understand that I can simply parse the JSON string with this command:

response = JSON.parse(response);

但是下一个单独访问每个对象的命令是什么? 我宁愿不使用jQuery.

But what is the next command to access each of the objects individually? I would prefer to not use jQuery.

推荐答案

在普通的JavaScript中,只需使用简单的for循环即可:

In plain JavaScript, just use a simple for loop:

for(var i = 0; i < response.length; i++){
    console.log(response[i]); // Object with id and time
}

-或-

response.forEach(function(object){
    console.log(object); 
});

演示

这篇关于在JavaScript中解析JSON以获取多个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆