Parsing JSON in javascript using jQuery
Let's say your JSON result is like this:
{ "playlist": { "track": { "song": "Wake me up", "albumart": "http://example.com/image.png", "videoid": "CDsKBof4iMA" }, "track": { "song": "Wake me up 2", "albumart": "http://example.com/image2.png", "videoid": "CDsKBof4iMA2" }, "track": { "song": "Wake me up 3", "albumart": "http://example.com/image3.png", "videoid": "CDsKBof4iMA3" } }}
UPDATE:
This json is invalid format, because it has multiple sub-objects with same property name.If you are able, change response from server into this format:
{ playlist: { tracks: [{ "song": "Wake me up", "albumart": "http://example.com/image.png", "videoid": "CDsKBof4iMA" }, { "song": "Wake me up 2", "albumart": "http://example.com/image2.png", "videoid": "CDsKBof4iMA2" }, { "song": "Wake me up 3", "albumart": "http://example.com/image3.png", "videoid": "CDsKBof4iMA3" }] }}
Then you'll be able to get each track
object from passed Array:
You should use you $.getJSON
function like this:
$.getJSON("api/playlist/get.php", function (data) { for (var key in myObj.playlist.tracks) { //do something with your track object console.log(myObj.playlist.tracks[key].song) }})
Here is JsFiddle for you: http://jsfiddle.net/zur4ik/Fy6ud/1/
Try this:
$.getJSON("api/playlist/get.php", function(data) { var responseObject = JSON.parse(data); // do stuff with responseObject});