Json Parsing
-
-
@jsulm said in Json Parsing:
What link?
The link where you wrote a class which manages the task to parse the above (invalid) json which was given to you? :-D
-
@Stephen28 You asked similar question here: https://forum.qt.io/topic/141143/how-to-parse-get-the-cities-array-which-is-in-object
What did you try already? You need to iterate over the "data" array and for each entry there check the name of the country. If you found the object with country=Afganistan you can get the cities just like in the link above...@jsulm said in Json Parsing:
You need to iterate over the "data" array and for each entry there check the name of the country. If you found the object with country=Afganistan you can get the cities just like in the link above...
This is what you need to do. Just try to spell the country you want correctly, unlike @jsulm's response ;-)
-
@jsulm said in Json Parsing:
You need to iterate over the "data" array and for each entry there check the name of the country. If you found the object with country=Afganistan you can get the cities just like in the link above...
This is what you need to do. Just try to spell the country you want correctly, unlike @jsulm's response ;-)
@JonB said in Json Parsing:
unlike @jsulm's response
Yeah, spelling/orthography was never my strength :-)
-
@JonB said in Json Parsing:
unlike @jsulm's response
Yeah, spelling/orthography was never my strength :-)
-
@Stephen28 You asked similar question here: https://forum.qt.io/topic/141143/how-to-parse-get-the-cities-array-which-is-in-object
What did you try already? You need to iterate over the "data" array and for each entry there check the name of the country. If you found the object with country=Afganistan you can get the cities just like in the link above... -
#include <QCoreApplication> #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QJsonValue> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFile in("/tmp/test/test.json"); if (in.open(QFile::ReadOnly)) { QByteArray json = in.readAll(); QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(json, &error); if (!doc.isNull()) { // We have valid JSON, but not necessarily a good response if (doc.isObject()) { QJsonObject top = doc.object(); if (top.contains("error")) { QJsonValue errorValue = top.value("error"); if (errorValue.isBool() && !errorValue.toBool(true)) { // Find Afghanistan (assuming the structure is as expected) QJsonArray dataArray = top.value("data").toArray(); for (auto dataEntry: dataArray) { if (dataEntry.isObject()) { QJsonObject countryObject = dataEntry.toObject(); // no error checking in next few lines, but you really should if (countryObject.value("country").toString() == QStringLiteral("Afghanistan")) { QJsonArray cityArray = countryObject.value("cities").toArray(); for (auto cityValue: cityArray) { qDebug() << cityValue.toString(); } } } } } } } } else { qDebug() << error.errorString(); } } return a.exec(); } -
#include <QCoreApplication> #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QJsonValue> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFile in("/tmp/test/test.json"); if (in.open(QFile::ReadOnly)) { QByteArray json = in.readAll(); QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(json, &error); if (!doc.isNull()) { // We have valid JSON, but not necessarily a good response if (doc.isObject()) { QJsonObject top = doc.object(); if (top.contains("error")) { QJsonValue errorValue = top.value("error"); if (errorValue.isBool() && !errorValue.toBool(true)) { // Find Afghanistan (assuming the structure is as expected) QJsonArray dataArray = top.value("data").toArray(); for (auto dataEntry: dataArray) { if (dataEntry.isObject()) { QJsonObject countryObject = dataEntry.toObject(); // no error checking in next few lines, but you really should if (countryObject.value("country").toString() == QStringLiteral("Afghanistan")) { QJsonArray cityArray = countryObject.value("cities").toArray(); for (auto cityValue: cityArray) { qDebug() << cityValue.toString(); } } } } } } } } else { qDebug() << error.errorString(); } } return a.exec(); } -
@Stephen28 What does this mean, what are you referring to in the code?