无法理解为什么我无法读取JSON

时间:2016-03-02 04:36:34

标签: c++ json qt

我有以下静态方法,允许我从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而不是飞机注册。为什么?

0 个答案:

没有答案