我已经编写了一个解析JSON文件并将代码保存在对象结构数组中的代码,如下所示:
/*DataModel.qml*/
Item {
id: dataModel
property var forecastData: []
function updateForecastFromJson(parsedJSONData) {
var index = 0
for(;index < 5; ++index) {
var temp = {
'temp_min': parsedJSONData.list[index].main.temp_min,
'temp_max': parsedJSONData.list[index].main.temp_max,
'weatherIconUrl': parsedJSONData.list[index].main.url,
'weatherCondition': parsedJSONData.list[index].main.condition,
'time': parsedJSONData.list[index].main.time
}
dataModel.forecastData.push(temp)
}
}
DataModel.qml 是一个singlton,我在如下所示的转发器中使用它来显示数据:
/*main.qml*/
Window {
Grid {
id: bottomGrid
Repeater {
model: [
{ day: DataModel.forecastData[0].time, high:
DataModel.forecastData[0].temp_max, low:
DataModel.forecastData[0].temp_min, sourceIcon:
DataModel.forecastData[0].weatherIconUrl },
{ day: DataModel.forecastData[1].time, high:
DataModel.forecastData[1].temp_max, low:
DataModel.forecastData[1].temp_min, sourceIcon:
DataModel.forecastData[1].weatherIconUrl },
{ day: DataModel.forecastData[2].time, high:
DataModel.forecastData[2].temp_max, low:
DataModel.forecastData[2].temp_min, sourceIcon:
DataModel.forecastData[2].weatherIconUrl },
{ day: DataModel.forecastData[3].time, high:
DataModel.forecastData[3].temp_max, low:
DataModel.forecastData[3].temp_min, sourceIcon:
DataModel.forecastData[3].weatherIconUrl },
{ day: DataModel.forecastData[4].time, high:
DataModel.forecastData[4].temp_max, low:
DataModel.forecastData[4].temp_min, sourceIcon:
DataModel.forecastData[4].weatherIconUrl }
]
Column {
width: bottomGrid.width / 5
spacing: dp(5)
Image {
source: modelData.sourceIcon
anchors.horizontalCenter: parent.horizontalCenter
sourceSize: dp(20)
}
Item {
width: 1
height: dp(5)
}
AppText {
text: modelData.high
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: sp(14)
}
AppText {
text: modelData.low
color: "#aaffffff"
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: sp(14)
}
Item {
width: 1
height: dp(5)
}
AppText {
text: modelData.day
anchors.horizontalCenter: parent.horizontalCenter
font.pixelSize: sp(14)
}
}
}
}
但是,当中继器即将显示一系列数据时, 什么都不会显示,我的输出如下:
TypeError:无法读取未定义的属性“时间”
我检查了几次数据是否正确解析,并且确定将数据正确存储在对象数组中。我哪里出问题了?