javascript - 访问多返回数据结构中的 JSON 对象

标签 javascript jquery json

如何编写下面的成功回调函数以便能够访问下面返回的 JSON 中的对象。显然,我将无法再使用 success: function(data) {if (data.returned === true) { 访问返回的对象。我该如何实现这个目标?

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"
        }
    ]
}

最佳答案

我不确定你在这里问什么。我认为你应该看看 javascript 命名空间。这样您就可以在对象(或命名空间)中创建属性并将 json 结果放入该属性中。

像这样:

 var myProjects = {
     projects: null,

     getProjects: function() {
           // do the ajax thing with something like
           myProjects.projects = data.projects;
     },

     placeProjects: function() {
           if (myProjects.projects == null) myProjects.getProjects();
           $.each(myProjects.projects, function(i,e){
                //place project content
           }
     },
 }

 // define the click event
 $("#projects").click(myProjects.placeProjects());

数据将被存储,直到您删除它或重新加载页面。您可以在 Firebug 的 DOM 检查器中看到该对象。希望有帮助

编辑:

我已经在这个 jsFiddle http://jsfiddle.net/BTbJu/5 中实现了这个想法 运行它,单击 Div 中的文本以加载第一个项目。持续点击即可旋转。

关于javascript - 访问多返回数据结构中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487010/

相关文章:

javascript - ExtJS 4.2 中欧洲货币输入的正则表达式

javascript - Rails 实时数据库查询跑马灯/股票代码

javascript - 如何在blade.php中使用外部.js文件中的代码?

json - Azure 逻辑应用 - 验证 JSON 文件中的内容

javascript - 替换字符串中的值

Javascript 字段验证表单

javascript - 如果 child 数多于一个数字,则在溢出时滚动

javascript - 我希望在用户单击播放按钮时更改电台名称

javascript - localstorage.getData 在电话间隙中无法正常使用 JSON

javascript - 在vuejs中访问数组内部数组的值