Search code examples
c++qt5

How to extract value from json array with QJsonDocument format


I'm getting a json format like this and I want to get the value of "Duration", "Id", "LoadCumulLimit" and "Notes".

QJsonDocument({"d":{"results":[{"Duration":"420.000","Id":"123456789XYZ","LoadCumulLimit":"15.000","NavWpNioshToOpNoish":{"__deferred":{"uri":"http://xxx/WorkplaceNOISHDataSet('123456789XYZ')/NavWpNioshToOpNoish"}},"Notes":"123456789XYZ","__metadata":{"id":"xxx/WorkplaceNOISHDataSet('123456789XYZ')","type":"xxx.WorkplaceNOISHData","uri":"xxx/WorkplaceNOISHDataSet('123456789XYZ')"}}]}})

I tried to do this but it doesn't work and it return empty with array `

QJsonDocument document = QJsonDocument::fromJson(content.toUtf8());
QJsonArray documentArray = document.array();

QStringList wordList;

for (const QJsonValue &i : documentArray)
{
    //qInfo() << i.toString() << endl;
    wordList << i.toString();
}

Could you guys give me a help or any suggest?


Solution

  • You could convert the QJsonDocument to a QVariant. Then you can use QVariantMap or QVariantList to walk the document and use the appropriate toString() or toDouble() to retrieve the values.

    The following is hard-coded to your JSON there are only minimal validation checks included. (i.e. it is a disclaimer that the code is presented for educational purposes only and made not be production ready).

    bool parse()
    {
        QString json = "{\"d\":{\"results\":[{\"Duration\":\"420.000\",\"Id\":\"123456789XYZ\",\"LoadCumulLimit\":\"15.000\",\"NavWpNioshToOpNoish\":{\"__deferred\":{\"uri\":\"http://xxx/WorkplaceNOISHDataSet('123456789XYZ')/NavWpNioshToOpNoish\"}},\"Notes\":\"123456789XYZ\",\"__metadata\":{\"id\":\"xxx/WorkplaceNOISHDataSet('123456789XYZ')\",\"type\":\"xxx.WorkplaceNOISHData\",\"uri\":\"xxx/WorkplaceNOISHDataSet('123456789XYZ')\"}}]}}";
        QJsonDocument document = QJsonDocument::fromJson(json.toUtf8());
        if (document.isEmpty() || document.isNull()) return false;
        QVariantMap root = document.toVariant().toMap();
        if (root.isEmpty()) return false;
        QVariantMap d = root["d"].toMap();
        if (d.isEmpty()) return false;
        QVariantList results = d["results"].toList();
        if (results.isEmpty()) return false;
        foreach (QVariant varResult, results)
        {
            QVariantMap result = varResult.toMap();
            if (result.isEmpty()) return false;
            bool ok = true;
            double duration = result["Duration"].toDouble(&ok);
            if (!ok) return false;
            QString id = result["Id"].toString();
            if (id.isEmpty() || id.isNull()) return false;
            double loadCumulLimit = result["LoadCumulLimit"].toDouble(&ok);
            if (!ok) return false;
            QString notes = result["Notes"].toString();
            if (!notes.isEmpty() || notes.isNull()) return false;
            qDebug() << id << duration << loadCumulLimit << notes; // "123456789XYZ" 420 15 "123456789XYZ"
        }
        return true;
    }
    

    Alternatively, you can just use QJsonDocument, QJsonValue and QJsonArray to walk the document and use the corresponding toString() and toDouble() to retrieve the values. Again, there are minimal validation checks included:

    bool parse2()
    {
        QString json = "{\"d\":{\"results\":[{\"Duration\":\"420.000\",\"Id\":\"123456789XYZ\",\"LoadCumulLimit\":\"15.000\",\"NavWpNioshToOpNoish\":{\"__deferred\":{\"uri\":\"http://xxx/WorkplaceNOISHDataSet('123456789XYZ')/NavWpNioshToOpNoish\"}},\"Notes\":\"123456789XYZ\",\"__metadata\":{\"id\":\"xxx/WorkplaceNOISHDataSet('123456789XYZ')\",\"type\":\"xxx.WorkplaceNOISHData\",\"uri\":\"xxx/WorkplaceNOISHDataSet('123456789XYZ')\"}}]}}";
        QJsonDocument document = QJsonDocument::fromJson(json.toUtf8());
        if (document.isEmpty() || document.isNull()) return false;
        QJsonValue d = document["d"];
        if (d.isNull() || d.isUndefined()) return false;
        QJsonArray results = d["results"].toArray();
        if (results.isEmpty()) return false;
        foreach (QJsonValue result, results)
        {
            double duration = result["Duration"].toDouble();
            QString id = result["Id"].toString();
            if (id.isEmpty() || id.isNull()) return false;
            double loadCumulLimit = result["LoadCumulLimit"].toDouble();
            QString notes = result["Notes"].toString();
            if (!notes.isEmpty() || notes.isNull()) return false;
            qDebug() << id << duration << loadCumulLimit << notes; // "123456789XYZ" 420 15 "123456789XYZ"
        }
        return true;
    }