0

I am trying to read a json file like shown below in QT. Can someone suggest a way to obtain values from json object and store them in separate containers or array such as test_cell2.CELLS[0] or some way so that nesting can also be taken care of and also I can access them easily after parsing the file

"test_cells2" : {
        "CELLS" : {
            "cell_0" : {
                "prettyName" : "cell_1",
                "CELLS" : {
                    "cell_1" : {
                        "prettyName" : "cell_1",
                        "type" : "default",
                    },
                },
            },
            "cell_1" : {
                "prettyName" : "cell_1",
                "type" : "default",
            },
            "cell_2" : {
                "type" : "text cell ko",
            },
            "cell_3" : {
                "prettyName" : "cell_3",
                "type" : "default",
            },
            "cell_4" : {
                "data" : {
                    "settings" : {
                        "EXEC_PARAMETERS" : {
                            "defaultQueue" : "batch",
                            "environment" : {
                                "blabla" : "blabla2",
                            },
                        },
                    },
                },
                "type" : "parallel_test",
             },
        },
    },
leo
  • 23
  • 1
  • 7
  • Possible duplicate of [How to create/read/write JSon files in Qt5](http://stackoverflow.com/questions/15893040/how-to-create-read-write-json-files-in-qt5) – jonspaceharper Aug 04 '16 at 13:38

2 Answers2

3

Take a look at this function

QJsonDocument::fromJson(QByteArray)

http://doc.qt.io/qt-5/qjsondocument.html#fromJson

Then use QJsonDocument::object() and you can use keys to get your values:

QJsonDocument doc = QJsonDocument::fromJson(QByteArray);
QJsonObject root = doc.object();
foreach(QJsonValue element, root["CELLS"].toArray()){
    QJsonObject node = element.toObject();
    node["whatEver"];

}
Marcus
  • 1,910
  • 2
  • 16
  • 27
1

If you Qt Version > 5.5 check QJSonDocument, http://doc.qt.io/qt-5/qjsondocument.html.

An Example:

    // Just read it from a file... or copy here        
    const QString yourJSON = "...";
    const QJsonDocument jsonDoc = QJsonDocument::fromJson(yourJSON.toLocal8Bit());
    const QVariantMap map = jsonDoc.toVariant().toMap();

To take care of nesting of CELLS inside CELLS, first load your CELLS array:

    // Start read your parameter
    const QVariant testCells2 = map["test_cells2"].toVariant();
    const QVariantList CELLS = testCells2.toList();
    // Now, we have a list available CELLS, check one be one
    foreach (const QVariant& cell, CELLS) {
        const QVariantMap wrapped = cell.toMap();
        qDebug() << "Pretty Name: " << wrapped["prettyName"].toString();
        qDebug() << "Type: " << wrapped["type"].toString();
        const hasList = !wrapped["CELLS"].toList().isEmpty();
        qDebug() << "Has child list? <<  hasList;
        if (hasList) {
             // Start another loop to check the child list.
        }
    }
mohabouje
  • 3,867
  • 2
  • 14
  • 28