如何编写下面的成功回调函数以便能够访问下面返回的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