Javascript不会正确解析JSON中的对象

时间:2015-11-04 12:45:44

标签: javascript json smartface.io

我正在尝试使用Repeat Box来填充JSON文档中的对象。但是,它不会添加每个项目,而是添加JSON文档中第一个项目的多个副本。关于为什么的任何线索?

我使用以下代码从我的JSON文档中提取对象。

function Project_WebClient1_OnSyndicationSuccess(e){
      var FactsArray = [];
    var parsedResponse = JSON.parse(this.responseText);
    var numOfFacts = parsedResponse.results.length;
    for (var i = 0; i < numOfFacts; i++) {
        var FactsObject = {};
        FactsObject.heading = parsedResponse.results[i].heading;
        FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate;   
        FactsObject.url = parsedResponse.results[i].url;      
        FactsArray.push(FactsObject);
        log(FactsObject.heading);
    }
            Pages.pgFundInfo.RepeatBox1.dataSource = FactsArray;
            Pages.pgFundInfo.RepeatBox1.refresh();
Data.notify("Data.FactSheets_OutDSetFactSheets.FactSheetsId");
}

以下是JSON文档:

{
    "results": [
        {
            "FactFile": {
                "__type": "File",
                "name": "Sept2015.pdf",
                "url": "http://files.sample.com/Sept2015.pdf"
            },
            "Heading": "Medium Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:29:00.000Z"
            },
            "createdAt": "2015-10-31T06:28:03.189Z",
            "objectId": "XUS4guS8Hu",
            "updatedAt": "2015-11-04T10:00:37.092Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "MedConsGrowthSept2015.pdf",
                "url": "http://files.sample.com/MedConsGrowthSept2015.pdf"
            },
            "Heading": "Medium-Conservative Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:30:00.000Z"
            },
            "createdAt": "2015-10-31T06:31:18.502Z",
            "objectId": "LOLyM2KX5g",
            "updatedAt": "2015-11-04T10:00:40.561Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "HiMedGrowthSept2015.pdf",
                "url": "http://files.sample.com/HiMedGrowthSept2015.pdf"
            },
            "Heading": "High-Medium Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:50:00.000Z"
            },
            "createdAt": "2015-10-31T06:50:54.367Z",
            "objectId": "I59ZKa5Onq",
            "updatedAt": "2015-11-04T10:00:47.268Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "HiGrowthSept2015.pdf",
                "url": "http://files.sample.com/HiGrowthSept2015.pdf"
            },
            "Heading": "High Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:52:00.000Z"
            },
            "createdAt": "2015-10-31T06:52:05.618Z",
            "objectId": "m4vkEDBgRr",
            "updatedAt": "2015-11-04T10:00:52.294Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "ConsGrowthSept2015.pdf",
                "url": "http://files.sample.com/ConsGrowthSept2015.pdf"
            },
            "Heading": "Conservative Growth September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:53:00.000Z"
            },
            "createdAt": "2015-10-31T06:53:27.399Z",
            "objectId": "bthR88Ck8y",
            "updatedAt": "2015-11-04T10:00:55.160Z"
        },
        {
            "FactFile": {
                "__type": "File",
                "name": "PerformerSept2015.pdf",
                "url": "http://files.sample.com/PerformerSept2015.pdf"
            },
            "Heading": "Multi-Asset Class Portfolio Range September",
            "ReleaseDate": {
                "__type": "Date",
                "iso": "2015-09-30T06:55:00.000Z"
            },
            "createdAt": "2015-10-31T06:55:32.021Z",
            "objectId": "JluOn1O0IO",
            "updatedAt": "2015-11-04T10:00:58.839Z"
        }
    ]
}

我得到了正确的结果数量,但是,在我的重复框中,它列出了它们,但都是使用第一个条目的数据,而不是其余的。

我在for循环中做错了什么?

2 个答案:

答案 0 :(得分:2)

根据您的JSON文档,循环的这一部分

FactsObject.heading = parsedResponse.results[i].heading;
FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate;   
FactsObject.url = parsedResponse.results[i].url;  

应改写为

FactsObject.heading = parsedResponse.results[i].Heading; // notice the capital H
FactsObject.ReleaseDate = parsedResponse.results[i].ReleaseDate; // maybe use the .iso attribute, if you don't want to assign the whole object  
FactsObject.url = parsedResponse.results[i].FactFile.url;  // notice the FactFile     

否则会得到undefined个结果。

但是我不确定是什么原因造成的问题只是获得第一项,而不是每一项。

修改

如果我只是跑

for (var i = 0, len = parsedResponse.results.length; i < len; i++) {
    console.log(parsedResponse.results[i]);
}

我得到每个单独的项目,而不是第一个多次。

请参阅http://jsfiddle.net/q2burjk3/

答案 1 :(得分:1)

@ Decay42建议的更正很好。

我不知道您的RepeatBox配置是什么样的。您可能需要定义onRowRender函数。

请参阅:https://gist.github.com/SmartfaceDocs/5aa40d93fd296a7d2ef9#file-codeblock-js-L27-L29

另外,我很好奇循环中的log函数是做什么的,因为我认为没有全局JS log函数。