简体   繁体   中英

How to parse this JSON file? (Qt)

Im using Qt5.7.1 and trying to read information in my JSON file. The structure of my file includes: the name of the configuration, the number of neurons, an array of neurons (each neuron has a strict number of receptors and synapses, which are also represented by arrays) and the coefficient values ​​for each of them. I need to get these values. I have this JSON file:

{
"Task config name": "Test",
"Configuration": {
    "NeuronsCount": 2,
    "Neurons": [
        {
            "ReceptorsCount": 3,
            "Receptors": [
                {
                    "coef1": 17.32,
                    "coef2": 11.992,
                    "coef3": 2.314
                },
                {
                    "coef1": 12.982,
                    "coef2": 96.148,
                    "coef3": -1.899
                },
                {
                    "coef1": 49.11,
                    "coef2": 35.001,
                    "coef3": -643.52
                }
            ],

            "SynapsysCount": 4,
            "Synapses": [
                {
                    "coef1": 13.22,
                    "coef2": 31.992,
                    "coef3": 22.314
                },
                {
                    "coef1": 12.81,
                    "coef2": 36.8,
                    "coef3": -53.189
                },
                {
                    "coef1": 1.11,
                    "coef2": 44.261,
                    "coef3": -23.12
                },
                {
                    "coef1": 642.86,
                    "coef2": 24.24,
                    "coef3": 95.009
                }
            ]
        },

        {
            "ReceptorsCount": 3,
            "Receptors": [
                {
                    "coef1": 6.32,
                    "coef2": 64.992,
                    "coef3": 98.314
                },
                {
                    "coef1": 42.982,
                    "coef2": 11.148,
                    "coef3": -12.899
                },
                {
                    "coef1": 1.11,
                    "coef2": 752.001,
                    "coef3": -3.82
                }
            ],

            "SynapsysCount": 4,
            "Synapses": [
                {
                    "coef1": 19.82,
                    "coef2": 1.592,
                    "coef3": 75.384
                },
                {
                    "coef1": 89.81,
                    "coef2": 65.8,
                    "coef3": -13.189
                },
                {
                    "coef1": 18.11,
                    "coef2": 11.261,
                    "coef3": -211.12
                },
                {
                    "coef1": 2.86,
                    "coef2": 8.24,
                    "coef3": 6.009
                }
            ]
        }
    ]
}

}

How can i receive values of coef# of each "Receptor" and "Synapse"? I tried this, but it return me 0.. How to read such a file?

QByteArray data = jsonFile.readAll();
QJsonDocument document;
document = document.fromJson(data);
QJsonObject jsonObject = document.object();
QJsonArray neuronsArray = jsonObject.value("Neurons").toArray();
qDebug() << "Size = " << neuronsArray.size();

you have to iterate through the JSON document:

QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
QJsonObject root = doc.object();
QJsonObject conf = root.value("Configuration").toObject();
//this gives you the neurons array, in there you have objects which you can access just like above
QJsonArray arr = conf.value("Neurons").toArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM