Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Json Parsing
Qt 6.11 is out! See what's new in the release blog

Json Parsing

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stephen28

    {"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

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2
    This post is deleted!
    1 Reply Last reply
    0
    • S Stephen28

      {"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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @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...

      S JonBJ 3 Replies Last reply
      2
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #4

        QJsonDocument, QJsonObject, QJsonArray

        First you will need valid JSON data.

        1 Reply Last reply
        3
        • S Offline
          S Offline
          Stephen28
          wrote on last edited by
          #5

          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.

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @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...

            S Offline
            S Offline
            Stephen28
            wrote on last edited by
            #6

            @jsulm can you share the link

            jsulmJ 1 Reply Last reply
            0
            • S Stephen28

              @jsulm can you share the link

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Stephen28 said in Json Parsing:

              can you share the link

              What link?

              Christian EhrlicherC 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Stephen28 said in Json Parsing:

                can you share the link

                What link?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @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

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • jsulmJ jsulm

                  @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...

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @Stephen28

                  @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 ;-)

                  jsulmJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Stephen28

                    @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 ;-)

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @JonB said in Json Parsing:

                    unlike @jsulm's response

                    Yeah, spelling/orthography was never my strength :-)

                    JonBJ 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @JonB said in Json Parsing:

                      unlike @jsulm's response

                      Yeah, spelling/orthography was never my strength :-)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      @jsulm
                      Don't give me that! How do you spell Afghanistan in German??

                      jsulmJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @jsulm
                        Don't give me that! How do you spell Afghanistan in German??

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @JonB Same. But the language doesn't matter: I'm not good at that :-) Not even in my native language.

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @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...

                          S Offline
                          S Offline
                          Stephen28
                          wrote on last edited by
                          #13

                          @jsulm can u please help me with the code

                          C 1 Reply Last reply
                          0
                          • S Stephen28

                            @jsulm can u please help me with the code

                            C Offline
                            C Offline
                            ChrisW67
                            wrote on last edited by ChrisW67
                            #14
                            #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();
                            }
                            
                            
                            
                            
                            S 1 Reply Last reply
                            1
                            • C ChrisW67
                              #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();
                              }
                              
                              
                              
                              
                              S Offline
                              S Offline
                              Stephen28
                              wrote on last edited by Stephen28
                              #15

                              @ChrisW67 can't able to use bool check for json object.

                              JonBJ 1 Reply Last reply
                              0
                              • S Stephen28

                                @ChrisW67 can't able to use bool check for json object.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #16

                                @Stephen28 What does this mean, what are you referring to in the code?

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved