2011-07-23 26 views
0

所以我試圖通過一個JSON響應循環,但我似乎無法得到它的權利。

段:

$.getJSON("/playlist/",function(playlists) { 
    $.each(playlists,function() { 
     self.playlists[this.playlist.id] = new SC.Playlist(this, self); 
     console.log(this); 
    }) 
}) 

JSON:

jsonp1311444173992([ 
    { 
    is_owner: true, 
    id: "wtf", 
    playlist: { 
     id: "latest1", 
     name: "Hot Tracks1", 
     version: 0, 
     tracks: "33+44+55" 
    } 
    }, 
    { 
    is_owner: true, 
    playlist: { 
     id: "latest", 
     name: "Hot Tracks", 
     smart: true, 
     version: 0, 
     smart_filter: { 
     order: "hotness" 
     } 
    } 
    }, 
    { 
    is_owner: true, 
    playlist: { 
     id: "latest3", 
     name: "Hot Tracks3", 
     version: 0, 
     tracks: "33+44+55" 
    } 
    }, 
    { 
    is_owner: true, 
    playlist: { 
     id: "latest4", 
     name: "Hot Tracks4", 
     version: 0, 
     tracks: "33+44+55" 
    } 
    }, 
    { 
    is_owner: true, 
    playlist: { 
     id: "latest5", 
     name: "Hot Tracks5", 
     version: 0, 
     tracks: "33+44+55" 
    } 
    } 
]); 

它只是似乎沒有下井JSON列表。我沒有正確調用其中一個變量嗎?

回答

5

這是無效的JSON。您需要引用您的密鑰:

{ 
    "is_owner": true, 
    "id": "wtf", 
    "playlist": { 
     "id": "latest1", 
     "name": "Hot Tracks1", 
     "version": 0, 
     "tracks": "33+44+55" 
    } 
    }, 

..等等。

未來項目的一個方便的引用是JSONLint JSON Validator

+0

+1用於指出無效的JSON和JSONLint。 –

+0

對不起,我們一起手工測試靜態響應。 – jiexi

0

首先,你向我們展示了JSON ....不完全是JSON。我不確定jQuery.getJSON是否可以讀取。

其次,我不知道self是做什麼的。我認爲它是指最外層的範圍,對吧?

第三,是太多,要求進行適當的縮進和運行的代碼?:

$.getJSON("/playlist/",function(playlists) { 
    $.each(playlists,function() { 
     self.playlists[this.playlist.id] = new SC.Playlist(this, self); 
     console.log(this); 
    } 
} 

順便說一句,我會做console.log(playlists,self,this),以確保一切正常。