2

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
        });      
    })
});
1
  • 1
    That would be JSONP, so yes, if you're trying to get that from another site you can use $.getJSON Commented Oct 4, 2013 at 18:21

1 Answer 1

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
    });        
})
Sign up to request clarification or add additional context in comments.

4 Comments

I have the literal JS object in php file json.php. I don't get any results if I use your code...
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.
Sorry but I don't know how to debug it :/
Can I shoe the result whit this: $("#data").html(data.type, data.data[0][0].artist) ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.