Json Parsing
-
{"error":false,"msg":"countries and cities retrieved","data":[{"iso2":"AF","iso3":"AFG","country":"Afghanistan","cities":["Herat","Kabul","Kandahar","Molah","Rana","Shar","Sharif","Wazir Akbar Khan"]},{"iso2":"AL","iso3":"ALB","country":"Albania","cities":["Elbasan","Petran","Pogradec","Shkoder","Tirana","Ura Vajgurore"]},{"iso2":"DZ","iso3":"DZA","country":"Algeria","cities":["Algiers","Annaba","Azazga","Batna City","Blida","Bordj","Bordj Bou Arreridj","Bougara","Cheraga","Chlef","Constantine","Djelfa","Draria","El Tarf","Hussein Dey","Illizi","Jijel","Kouba","Laghouat","Oran","Ouargla","Oued Smar","Relizane","Rouiba","Saida","Souk Ahras","Tamanghasset","Tiaret","Tissemsilt","Tizi","Tizi Ouzou","Tlemcen"]},{"iso2":"AD","iso3":"AND","country":"Andorra","cities":["Andorra la Vella","Canillo","Encamp","Engordany","Escaldes-Engordany","La Massana","Llorts","Ordino","Santa Coloma","Sispony","Soldeu"]}}
how to get the cities name according to country name,if country name=Afghanistan ,how can i get its cities name
-
{"error":false,"msg":"countries and cities retrieved","data":[{"iso2":"AF","iso3":"AFG","country":"Afghanistan","cities":["Herat","Kabul","Kandahar","Molah","Rana","Shar","Sharif","Wazir Akbar Khan"]},{"iso2":"AL","iso3":"ALB","country":"Albania","cities":["Elbasan","Petran","Pogradec","Shkoder","Tirana","Ura Vajgurore"]},{"iso2":"DZ","iso3":"DZA","country":"Algeria","cities":["Algiers","Annaba","Azazga","Batna City","Blida","Bordj","Bordj Bou Arreridj","Bougara","Cheraga","Chlef","Constantine","Djelfa","Draria","El Tarf","Hussein Dey","Illizi","Jijel","Kouba","Laghouat","Oran","Ouargla","Oued Smar","Relizane","Rouiba","Saida","Souk Ahras","Tamanghasset","Tiaret","Tissemsilt","Tizi","Tizi Ouzou","Tlemcen"]},{"iso2":"AD","iso3":"AND","country":"Andorra","cities":["Andorra la Vella","Canillo","Encamp","Engordany","Escaldes-Engordany","La Massana","Llorts","Ordino","Santa Coloma","Sispony","Soldeu"]}}
how to get the cities name according to country name,if country name=Afghanistan ,how can i get its cities name
This post is deleted! -
{"error":false,"msg":"countries and cities retrieved","data":[{"iso2":"AF","iso3":"AFG","country":"Afghanistan","cities":["Herat","Kabul","Kandahar","Molah","Rana","Shar","Sharif","Wazir Akbar Khan"]},{"iso2":"AL","iso3":"ALB","country":"Albania","cities":["Elbasan","Petran","Pogradec","Shkoder","Tirana","Ura Vajgurore"]},{"iso2":"DZ","iso3":"DZA","country":"Algeria","cities":["Algiers","Annaba","Azazga","Batna City","Blida","Bordj","Bordj Bou Arreridj","Bougara","Cheraga","Chlef","Constantine","Djelfa","Draria","El Tarf","Hussein Dey","Illizi","Jijel","Kouba","Laghouat","Oran","Ouargla","Oued Smar","Relizane","Rouiba","Saida","Souk Ahras","Tamanghasset","Tiaret","Tissemsilt","Tizi","Tizi Ouzou","Tlemcen"]},{"iso2":"AD","iso3":"AND","country":"Andorra","cities":["Andorra la Vella","Canillo","Encamp","Engordany","Escaldes-Engordany","La Massana","Llorts","Ordino","Santa Coloma","Sispony","Soldeu"]}}
how to get the cities name according to country name,if country name=Afghanistan ,how can i get its cities name
@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... -
QJsonDocument, QJsonObject, QJsonArray
First you will need valid JSON data.
-
I was Learning qt. I am Parsing json values from https://countriesnow.space/api/v0.1/countries this api services. I am Parsing country values to one combo Box and If the Country is selected,add the cities name to another combo Box.
-
@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:
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?