Coldfusion:serializeJSON

时间:2014-07-30 14:38:31

标签: ajax json coldfusion

我正在开发项目,其中文章通过json加载到滚动条中。 AJAX:

if ($("body#homepage").length) {
        var homepageScroller = new Scroller();
        homepageScroller.url = "http://superyachttimes.2.label-a.nl/default/includes/themes/SuperyachtTimes/assets/js/homepage.json";
        homepageScroller.handlebarsSource = "#article-holder";
        homepageScroller.htmlTemplate = "#holder";
        homepageScroller.jsonKey = "mainlayout";
        homepageScroller.sort = function(response) {
            var data = {};
            data["templateThree"] = [response[0],response[1],response[2]];
            data["templateFour"] = [response[3], response[4], response[5], response[6]];
            return data;
        }

        $(".loadmore-holder a").on("click", function(e) {
            e.preventDefault();
            $(this).bind("click", homepageScroller.clicked());
        });
    }

网址指向我的homepage.json,其结构如下:

 "status": "SUCCESS",
    "currentPage": "1",
    "photos" : [
        {
             "articleImage": "assets/img/yacht-photo.jpg",
             "title": "Superyacht Aurora at anchor in Porto Heli",
             "date": "Monday, 14 April"
        }
]

唯一的问题是这个json是硬编码的。我需要它是动态的,因为它会不断加载到文件中,所以它是无限的。

我使用以下代码从ColdFusion获取json输出:

<cfprocessingdirective suppresswhitespace="yes">
<cfsetting showdebugoutput="yes"> 
<cfscript>
       data = {
           title="#item.getValue('title')#", 
           date="#DateFormat($.content('created'),'dddd, dd mmmm yyyy')#",
           description="#item.getValue('summary')#",
           articleImage="#fileurl#",
           url="#$.createHREF(filename=item.getValue('filename'))#"
       };
       serializedStr = serializeJSON(data, true);
</cfscript>
</cfprocessingdirective>

...这给了我这个:

{
    "title":"Artikel vandaag 1"
    ,"description":"<p>Artikel artikel artikel artikel artikel<\/p>"
    ,"date":"Wednesday, 09 July 2014"
    ,"url":"\/artikelen\/artikel-vandaag-122\/"
    ,"articleImage":"\/default\/cache\/file\/0C59DDE5-1171-4F23-87514BA123D53A54.jpg"
}

如何解析我通过迭代进入json文件的信息?

1 个答案:

答案 0 :(得分:2)

所以假设这个处理程序期望response是一个至少包含7个元素的数组:

    homepageScroller.sort = function(response) {
        var data = {};
        data["templateThree"] = [response[0],response[1],response[2]];
        data["templateFour"] = [response[3], response[4], response[5], response[6]];
        return data;
    }

现在你拥有的是一个结构:

{
    "title": "Artikel vandaag 1",
    "description": "<p>Artikel artikel artikel artikel artikel<\/p>",
    "date": "Wednesday, 09 July 2014",
    "url": "\/artikelen\/artikel-vandaag-122\/",
    "articleImage": "\/default\/cache\/file\/0C59DDE5-1171-4F23-87514BA123D53A54.jpg"
}

因此,您应该可以引用response.titleresponse['description']

之类的内容

虽然老实说我不确定你的Scroller类ajax请求和处理ajax响应的sort函数如何,与你声明的主页json相对应。响应是你的照片吗?