0

I have a json object and I want to loop through the items in the object and display their data. when I do a console log of the json object I get the following in the console:

json object

My question is, how do I loop through this data and expose the 'name' 'songId' and 'upvote'?

I've tried the following

var json = JSON.parse(jsonObject);
console.log(json);
$.each(json, function (key, value) {
    console.log(value);

    //   console.log(value.name[key]);
});

Any help or suggestions would be great!

0

4 Answers 4

2

From the screenshot it looks like what you want to iterate is json.playlists not json

var json = JSON.parse(jsonObject);
    console.log(json.playlists);
    $.each(json.playlists, function (key, value) {
        console.log(value.songID);
        console.log(value.name);
    });
Sign up to request clarification or add additional context in comments.

Comments

2

This is an object containing an array of objects, so to loop through it you need something like this:

for (var i = 0; i < json.playlists.length; i++) {
    console.log(json.playlists[i].name);
    console.log(json.playlists[i].songID);
}

Comments

2

Your json is an object having a key named playlists as array.

Something like bellow

json = {playlists:[{name:"song1",songID:"1"},....]}

You have to iterate on this array.

This should work for you.

json.playlists.forEach(function (playlist) {
   console.log(playlist.name);
   console.log(playlist.songID);
})

Comments

1

You should iterate from json.playlists, since the structure is:

Object{
  playlists: [
    {},
  ]
}


$.each(json.playlists, function (key, value) {
  // stuffs to do here
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.