简体   繁体   中英

JavaScript Json Array foreach?

So I'm trying to parse this json data into lists on my site. However I can't seem to loop. I've looked online and nothing seems to work...

JsonData:

{"amount":4998,"users":[{"id":"765","username":"test","profileURL":"http://example.com","avatarURL":"http:///example.com"},{"id":"765","username":"test","profileURL":"http://example.com","avatarURL":"http://example.com"}]}

I want to loop users and then get id, username etc...

obj.users.forEach(function(user) {
  console.log(user.username);
});

With a basic for loop:

for (var i = 0; i < obj.users.length; i++) {
    var user = obj.users[i];
    console.log(user.username);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM