Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Json Parsing
Qt 6.12 LTS is out! See what's new in the release blog

Json Parsing

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 3.0k 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.
  • 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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          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 :-)

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                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.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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
                            • Unsolved