Skip to content Skip to sidebar Skip to footer

How To Loop On A Json Object?

I have this JSON object: {'time':'123456789', 'raw':'chat_history', 'data':{ 'msg':[ {'time':1111111111, 'user':'user1', 'text':'text from user1'}, {'time':2222222222, 'user':'us

Solution 1:

for(var i = 0; i < data.msg.length; i++)
{
    var row = data.msg[i];
    print(row.time + ' - ' + row.user + ' - ' + row.text);
}

Solution 2:

var msg = yourJsonObject.data.msg;

for (var i = 0, len = msg.length; i < len; i++) {
  // document.write only for example
  document.write(msg[i].time + ' - ' + msg[i].user + ' - ' + msg[i].text);
}

Post a Comment for "How To Loop On A Json Object?"