Javascript 无法正确解析来自 JSON 的对象

Javascript wont parse objects from JSON correctly

我正在尝试获取一个重复框来填充 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 循环中做错了什么?

根据您的 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/

@Decay42 建议的更正很好。

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

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

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