Jquery循环数据

时间:2013-10-04 18:18:48

标签: jquery json loops

如何将以下JSON数据字符串循环到jquery? 我应该使用函数$ .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]})

我尝试使用此代码:

$(document).ready(function() {
    $.getJSON("json.php", function(data){
        $.each(data.data[0], function(i, item){
            $("#data").append(item.artist); // Name1, Name2
        });      
    })
});

1 个答案:

答案 0 :(得分:1)

这不是JSON,它是一个文字JS对象,因为jQuery已经解码了它。只需使用括号[],点符号.和循环。

在回调中,如果参数名称是数据,则可以执行以下操作

$.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
    });        
})