Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON in javascript for multiple JSON objects [closed]

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

Using client-side javascript:

My client receives some JSON string:

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

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

response = JSON.parse(response);

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

like image 341
ace Avatar asked Sep 18 '13 16:09

ace


1 Answers

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
}

-or-

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

Demo

like image 74
Eric Hotinger Avatar answered Nov 15 '22 15:11

Eric Hotinger