我有以下静态方法,允许我从JSON中获取Airplane
对象:
Airplane AirplaneManager::load(QString airplaneRegistration) {
QFile loadFile("airplanes/"+airplaneRegistration+".json");
if (!loadFile.open(QIODevice::ReadOnly)) {
Airplane temp;
return temp;
}
QJsonDocument loadDoc(QJsonDocument::fromJson(loadFile.readAll()));
QJsonObject airplane = loadDoc.object()["plane"].toObject();
Airplane temp(airplane["registration"].toString(), airplane["type"].toString()); // creates the airplane object
// adds some details
temp.setDateFormat(airplane["dateFormat"].toString());
temp.setCreatedDate(QDate::fromString(airplane["createdDate"].toString(), temp.getDateFormat()));
return temp;
}
JSON就是这个:
{
"F-FBZH": {
"createdDate": "29/02/2016",
"registration": "F-FBZH",
"type": "DR400-120"
}
}
当我尝试访问airplane["registration"].toString()
时,它返回一个空的QString而不是飞机注册。为什么?