c++jsonqtqt5.7

Parse nested JSON with QJsonDocument in Qt


I am interested in seeing how we can use Qt's QJsonDocument to parse all entries from a simple nested JSON (as I have just started studying this).

nested json example:

{
    "city": "London",
    "time": "16:42",
    "unit_data": 
        [
            {
                "unit_data_id": "ABC123",
                "unit_data_number": "21"
            },
            {
                "unit_data_id": "DEF456",
                "unit_data_number": "12"
            }
        ]
}

I can parse the non-nested parts of it like so:

QJsonObject jObj;
QString city = jObj["city"].toString();
QString time = jObj["time"].toString();

Solution

  • I am not sure what you are asking, but perhaps this might help:

    QJsonDocument doc;
    doc = QJsonDocument::fromJson("{                                              "
                                  "     \"city\": \"London\",                      "
                                  "     \"time\": \"16:42\",                       "
                                  "     \"unit_data\":                             "
                                  "         [                                      "
                                  "             {                                  "
                                  "                 \"unit_data_id\": \"ABC123\",  "
                                  "                 \"unit_data_number\": \"21\"   "
                                  "             },                                 "
                                  "             {                                  "
                                  "                 \"unit_data_id\": \"DEF456\",  "
                                  "                 \"unit_data_number\": \"12\"   "
                                  "             }                                  "
                                  "         ]                                      "
                                  " }");
    
    // This part you have covered
    QJsonObject jObj = doc.object();
    qDebug() << "city" << jObj["city"].toString();
    qDebug() << "time" << jObj["time"].toString();
    // Since unit_data is an array, you need to get it as such
    QJsonArray array = jObj["unit_data"].toArray();
    // Then you can manually access the elements in the array
    QJsonObject ar1 = array.at(0).toObject();
    qDebug() << "" << ar1["unit_data_id"].toString();
    // Or you can loop over the items in the array
    int idx = 0;
    for(const QJsonValue& val: array) {
        QJsonObject loopObj = val.toObject();
        qDebug() << "[" << idx << "] unit_data_id    : " << loopObj["unit_data_id"].toString();
        qDebug() << "[" << idx << "] unit_data_number: " << loopObj["unit_data_number"].toString();
        ++idx;
    }
    

    The output I get is:

    city "London"
    time "16:42"
    "ABC123"
    [ 0 ] unit_data_id    :  "ABC123"
    [ 0 ] unit_data_number:  "21"
    [ 1 ] unit_data_id    :  "DEF456"
    [ 1 ] unit_data_number:  "12"