-2

Below part of json response, how can i get to objects in rows object, i need to do loop for all id and others attributes. , it is'nt a array so active_chats.rows[1].id not work. Thanks in advance for answers

 {
"active_chats":{
"rows":{
"2":{
"id":"2",
"nick":"bart",
"status":"1",
"time":"1453463784",
"user_id":"2",
"hash":"183c12afef48ea9942e5c0a7a263ef441039d832",
"ip":"::1",
"dep_id":"2",
"support_informed":"1",
"has_unread_messages":"1",
"last_user_msg_time":"1453476440",
"last_msg_id":"11",
"wait_time":"5171",
"user_tz_identifier":"Europe/Paris",
"nc_cb_executed":"1",
"user_closed_ts":"1453470674",
"department_name":"ECODEMO"
},
"3":{
"id":"3",
"nick":"robert",
"status":"1",
"time":"1453470058",
"user_id":"2",
"hash":"0fae69094667e452b5401552541602d5c2bd73ef",
"ip":"127.0.0.1",
"dep_id":"2",
"user_status":"1",
"support_informed":"1",
"user_typing":"1453479978",
"user_typing_txt":"Gość opuścił chat!",
"last_msg_id":"10",
"wait_time":"3285",
"user_tz_identifier":"Europe/Paris",
"nc_cb_executed":"1",
"user_closed_ts":"1453479983",
"unanswered_chat":"1",
"department_name":"ECODEMO"
}
},
"size":2
Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48

2 Answers2

1

Just do

for (var key in p) {
    if (p.hasOwnProperty(key)) {
    console.info(key + " => " + p[key]);
    }
}

where p is your json response object

did some tests here, your json is invalid.. but if fixed:

for(var row in response.active_chats.rows)
    {
    for (var key in response.active_chats.rows[row]) {
        console.log(key + " => " + response.active_chats.rows[row][key]);
    }
}

fiddle example (printing to console)

should do the trick

rmjoia
  • 962
  • 12
  • 21
0

In order to access the id you would have to do:

var number = 2
var idVal = obj.active_chats.rows[number].id; // idVal = 2

Here obj being whatever variable you saved the JSON in. Looping through the length of active_chats and rows would then help you step through each value.

Also, you can toss your JSON to the text box on this site to get a better picture of what is going on: http://jsbeautifier.org

tedcurrent
  • 431
  • 3
  • 13