如何編寫下面的成功回調函數以便能夠訪問下面返回的JSON中的對象。顯然,我將無法再使用success: function(data) {if (data.returned === true) {
訪問返回的對象。我怎麼做到這一點?在多返回的數據結構中訪問JSON對象
jQuery代碼:
$("#projects").click(function() {
jQuery.ajax({ type: "POST", dataType: "JSON",
url: "<?=base_url()?>index.php/home/projectsSlider",
json: {returned: true}, success: function(data) {
if (data.returned === true) {
$("#resultProjects").html(JSON.stringify(data.Projects));
$("#resultScreenshots").html(JSON.stringify(data.Screenshots));
$("#content").fadeOut(150, function() {
$(this).replaceWith(projectsSlider(data.projectId, data.projectName, data.startDate, data.finishedDate, data.projectDesc, data.createdFor, data.contributors, data.screenshotURI, data.websiteURL), function() {
$(this).fadeIn(150);
});
});
}
}
});
});
返回的JSON:
{
"Projects": [
{
"projectId": "932713684f9073189ec7b",
"projectName": "Cloud859Collective",
"startDate": "April 19th, 2012",
"finishedDate": "April 25th, 2012",
"createdFor": "ClasskCreations",
"contributors": "Mike Grigsby",
"projectDesc": "This website was created with a friend in mind. His name is Kevin Johnson and he is a rapper. He needed a website that would allow him to host and share his music."
},
{
"projectId": "10599012654f907093714e9",
"projectName": "Nurbell Studio",
"startDate": "April 15th, 2012",
"finishedDate": "April 19th, 2012",
"createdFor": "Nurbell LLC",
"contributors": "Mike Grigsby",
"projectDesc": "This is the page you are currently looking at. This is the official Nurbell homepage. Complete with a frontend and a backend."
}
],
"Screenshots": [
{
"screenshotURI": "http://nurbell.com/vd/1.0/images/project-data/kevo.png"
},
{
"screenshotURI": "http://nurbell.com/vd/1.0/images/project-data/nurbell.png"
}
]
}
http://pastebin.com/j6XncXmd - 像這樣?我收到錯誤:'未捕獲的語法錯誤:意外的令牌}'。而且我應該如何訪問'projectsSlider()'中的'data.projectId'? – 2012-07-14 21:13:32
好吧,每個函數在第一個參數(它是myProjects.projects)中遍歷數組中的所有元素,然後使用兩個參數在未命名函數中傳遞該元素:i =數組中元素的索引(如果您想要改變像:myProjects.projects [i])和第二個元素e = element是元素的內容。所以你可以通過e.projectId來引用projectId ....(你也可以將e重命名爲函數參數中的數據,但這會讓事情混淆,我想http://pastebin.com/PiDBBB0S – Jeroen 2012-07-14 21:43:19
發現了另一個錯字,也改變了縮進的可讀性:http://pastebin.com/AcDBKtCR – Jeroen 2012-07-14 21:52:57