Read from Json in an order
-
wrote on 8 Feb 2021, 14:55 last edited by
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(); } }
-
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(); } }
wrote on 8 Feb 2021, 14:59 last edited by KroMignon 2 Aug 2021, 15:00@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. -
@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.wrote on 8 Feb 2021, 15:02 last edited by@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.
-
@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.
wrote on 8 Feb 2021, 15:06 last edited by@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... -
@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.
wrote on 8 Feb 2021, 15:08 last edited by@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();
-
@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();
wrote on 9 Feb 2021, 05:57 last edited by@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
-
Lifetime Qt Championwrote on 9 Feb 2021, 06:01 last edited by Christian Ehrlicher 2 Sept 2021, 06:12
@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.
-
@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:
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
-
@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
wrote on 9 Feb 2021, 06:35 last edited by@jsulm thank you. I did it with your solution. It works fine.
1/9