Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

javascript - 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":"user2", "text":"text from user2"},
 {"time":3333333333, "user":"user3", "text":"text from user3"},
 {"time":4444444444, "user":"user4", "text":"text from user4"}
]
}}

I have to create a FOR to loop the elements of data.msg and print it:

I would print these results with the FOR:

11111111111 - user1 - text from users1
22222222222 - user2 - text from users2
33333333333 - user3 - text from users3
44444444444 - user4 - text from users4

Could you help me?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...