Read from Json in an order
-
I have a QJsonArray. Here is my json file. How can i get my datas from array in an order?
{ "ucus plani": [ { "bslngc": 0, "dgsm": 0, "rtno": 0, "tplm": 3 }, { "byl": 32.706642150878906, "enl": 40.0082893371582, "grv": 0, "irtf": 2500, "no": 0, "snrk": 1, "yrcp": 0 }, { "byl": 32.71247863769531, "enl": 39.98606491088867, "grv": 0, "irtf": 2500, "no": 1, "snrk": 2, "yrcp": 0 }, { "byl": 32.729129791259766, "enl": 40.00342559814453, "grv": 0, "irtf": 2500, "no": 2, "snrk": 0, "yrcp": 0 } ] }
int main(int argc, char *argv[]) { QApplication a(argc, argv); QStringList fileNames; fileNames = QFileDialog::getOpenFileNames(nullptr, ("Uçuş Planı Seçiniz"), "/home/ilknur/yi/up.sakla", ("Uçuş Planı(*.upd)")); for (auto xfile : fileNames) { QFile file (xfile); file.open(QIODevice::ReadOnly | QIODevice::Text); QByteArray data = file.readAll(); QJsonDocument doc = QJsonDocument::fromJson(data); QJsonObject root = doc.object(); QJsonArray planArray = root.value("ucus plani").toArray(); for(const auto &value : planArray) { qDebug() << QJsonObject object = value.ToObject(); } }
-
@suslucoder said in Read from Json in an order:
How can i get my datas from array in an order?
Which kind of order do mean?
Values in JSON objects are unordered only the objects in an array are ordered (by index starting with 0), AFAIK. -
@KroMignon for example, i want to see firsly
{ "byl": 32.706642150878906, "enl": 40.0082893371582, "grv": 0, "irtf": 2500, "no": 0, "snrk": 1, "yrcp": 0 },
which is the second part of my QJsonArray.
-
@suslucoder said in Read from Json in an order:
which is the second part of my QJsonArray.
As @KroMignon replied, the order is given but the way the JSON content is.
So if you know you want to start with second element of the array (index 1) then go and start processing the array at such index... -
@suslucoder said in Read from Json in an order:
for example, i want to see firsly
which is the second part of my QJsonArray.I can not understand what your problem is.
when reading a JSON document withQJsonDocument::fromJson()
, all items are parsed and stored.
If you want to read the second item of an array, it is up to you:JsonDocument doc = QJsonDocument::fromJson(data); QJsonObject root = doc.object(); QJsonArray planArray = root.value("ucus plani").toArray(); QJsonObject secondItem = planArray[1].toObject();
-
@KroMignon thank you for your answer. It works. After doing that,
QJsonObject secondItem = planArray[1].toObject();
How can i got the values in planArray[1] . It has some values like latitude longitude
{ "byl": 32.706642150878906, "enl": 40.0082893371582, "grv": 0, "irtf": 2500, "no": 0, "snrk": 1, "yrcp": 0 },
can i call all of them separately
-
@suslucoder said in Read from Json in an order:
How can i got the values in planArray[1] .
You already answered this by your self e.g. here: https://forum.qt.io/topic/123496/how-to-parse-such-a-complex-json-file/5 and the other way round was explained here: https://forum.qt.io/topic/123484/creating-json-file-with-subroot/13
Or simply read the documentation or the json savegame example which shows you exactly how to parse such simple stuff.
-
@suslucoder said in Read from Json in an order:
can i call all of them separately
Of course you can! In the same way you do with any JSON object:
planArray[1].toObject()["enl"];
Simply read https://doc.qt.io/qt-5/qjsonobject.html