我正在尝试获取重复框来填充 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
否则它将得到未定义
结果。
但我不确定是什么导致了只获取第一项而不是每一项的问题。
编辑:
如果我只是运行
for (var i = 0, len = parsedResponse.results.length; i < len; i++) {
console.log(parsedResponse.results[i]);
}
我多次获得每个单独的项目,而不是第一个项目。
关于Javascript 无法正确解析 JSON 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522331/