Home > front end >  How to extract value from json array with QJsonDocument format
How to extract value from json array with QJsonDocument format

Time:11-10

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?

CodePudding user response:

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 minimal validation checks included:

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;
}

CodePudding user response:

You have:

    object d {
        object results {
            [ { several objects to be extracted} ]
        }
    }

To extract a value of an object by given a key, call operator[](key) on QJsonValue. When you have an array, to extract its first item call operator[](0) on this array. When you have found an object at desired key, you can convert its value to the value of specified type by toString/toInt/toDouble etc. methods of QJsonValue.

Short version:

    QJsonValue item0 = document["d"]["results"].toArray()[0];
    QStringList wordList;
    wordList << item0["Duration"].toString() << item0["Id"].toString() << item0["LoadCumulLimit"].toString() << item0["Notes"].toString();

the longer version:

    QJsonValue dObj = document["d"];
    QJsonValue resultsObj = dObj["results"];
    QJsonArray resultsArray = resultsObj.toArray();
    QJsonValue item0 = resultsArray[0];
    QStringList wordList;
    wordList << item0["Duration"].toString() << item0["Id"].toString() << item0["LoadCumulLimit"].toString() << item0["Notes"].toString();
  • Related