How can i loop the following JSON data string into jquery? Should I use the function $.getJSON ?
jQuery17209521235961001366_1380903443191({"type":"result","rid":"hopkinsa","data":
[[{"artist":"NAME1","title":"SONG1","album":null,"royaltytrackid":null,"url":null,"image":"test1.jpg","time":1380910069,"localtime":"11:07 AM"},
{"artist":"NAME2","title":"SONG2","album":null,"royaltytrackid":null,"url":null,"image":"test2.jpg","time":1380909866,"localtime":"11:04 AM"},
{"artist":"NAME2","title":"SONG3","album":null,"royaltytrackid":null,"url":null,"image":"test3.jpg","time":1380909864,"localtime":"11:04 AM"}],false,0,10,0]})
I trying with this code:
$(document).ready(function() {
$.getJSON("json.php", function(data){
$.each(data.data[0], function(i, item){
$("#data").append(item.artist); // Name1, Name2
});
})
});
Ruan Mendes
92.7k31 gold badges162 silver badges225 bronze badges
-
1That would be JSONP, so yes, if you're trying to get that from another site you can use $.getJSONadeneo– adeneo2013年10月04日 18:21:56 +00:00Commented Oct 4, 2013 at 18:21
1 Answer 1
That's not JSON, it's a literal JS object because jQuery will already have decoded it. Just use brackets [], dot notation ., and loops.
In your callback, if the param name is data, you could do the following
$.getJSON("url.json", function(data){
console.log(data.type, data.data[0][0].artist) //"result", "Name1"
$.each(data.data[0], function(i, item){
console.log(item.artist); // Name1, Name2
});
})
answered Oct 4, 2013 at 18:22
Ruan Mendes
92.7k31 gold badges162 silver badges225 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Stefan
I have the literal JS object in php file json.php. I don't get any results if I use your code...
Ruan Mendes
Stefan, please provide actual error messages, the page is blank is not enough information to know what is going on. Did you check the network tab? Is the request going out? Did you debug it? Step through the code? Please do some debugging on your own and post it in your answer.
Stefan
Sorry but I don't know how to debug it :/
Stefan
Can I shoe the result whit this: $("#data").html(data.type, data.data[0][0].artist) ?
lang-js