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. Parsing JSON file
Qt 6.11 is out! See what's new in the release blog

Parsing JSON file

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 3.9k Views
  • 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.
  • Christian EhrlicherC Christian Ehrlicher

    And you still not show us how you try to access the single array elements nor what the debug output suggested by @JonB prints out.

    J Offline
    J Offline
    jenya7
    wrote on last edited by
    #13

    @Christian-Ehrlicher said in Parsing JSON file:

    And you still not show us how you try to access the single array elements nor what the debug output suggested by @JonB prints out.

    I still don't know how to do it. :)

    KroMignonK 1 Reply Last reply
    0
    • J jenya7

      @Christian-Ehrlicher said in Parsing JSON file:

      And you still not show us how you try to access the single array elements nor what the debug output suggested by @JonB prints out.

      I still don't know how to do it. :)

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #14

      @jenya7 said in Parsing JSON file:

      I still don't know how to do it. :)

      Yes, Qt documentation about JSON is a little bit short (https://doc.qt.io/qt-5/json.html).
      You can iterate through the array:

      
      for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
      {
          // convert value to object
          QJsonObject day = dayValue.toObject();
          // according to your post, temperature seems to be an object, not an array
          QJsonObject temperature = day["Temperature"].toObject();
      
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      J 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @jenya7 said in Parsing JSON file:

        I still don't know how to do it. :)

        Yes, Qt documentation about JSON is a little bit short (https://doc.qt.io/qt-5/json.html).
        You can iterate through the array:

        
        for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
        {
            // convert value to object
            QJsonObject day = dayValue.toObject();
            // according to your post, temperature seems to be an object, not an array
            QJsonObject temperature = day["Temperature"].toObject();
        
        }
        
        J Offline
        J Offline
        jenya7
        wrote on last edited by
        #15

        @KroMignon said in Parsing JSON file:

        @jenya7 said in Parsing JSON file:

        I still don't know how to do it. :)

        Yes, Qt documentation about JSON is a little bit short (https://doc.qt.io/qt-5/json.html).
        You can iterate through the array:

        
        for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
        {
            // convert value to object
            QJsonObject day = dayValue.toObject();
            // according to your post, temperature seems to be an object, not an array
            QJsonObject temperature = day["Temperature"].toObject();
        
        }
        

        Thank you. It works. And how to get objects inside the "Temperature"?
        "Temperature"->"Maximum"->"Value"
        "Temperature"->"Minimum"->"Value"
        I wonder why I can't access it like this

        QJsonArray temp = daily[0].toArray();
        

        I get no errors but it doesn't work.

        KroMignonK JonBJ 2 Replies Last reply
        0
        • J jenya7

          @KroMignon said in Parsing JSON file:

          @jenya7 said in Parsing JSON file:

          I still don't know how to do it. :)

          Yes, Qt documentation about JSON is a little bit short (https://doc.qt.io/qt-5/json.html).
          You can iterate through the array:

          
          for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
          {
              // convert value to object
              QJsonObject day = dayValue.toObject();
              // according to your post, temperature seems to be an object, not an array
              QJsonObject temperature = day["Temperature"].toObject();
          
          }
          

          Thank you. It works. And how to get objects inside the "Temperature"?
          "Temperature"->"Maximum"->"Value"
          "Temperature"->"Minimum"->"Value"
          I wonder why I can't access it like this

          QJsonArray temp = daily[0].toArray();
          

          I get no errors but it doesn't work.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #16

          @jenya7 said in Parsing JSON file:

          I get no errors but it doesn't work.

          Why should it work?
          You are trying to convert QJsonValue to QJsonArray, but according to your post it is a QJsonObject.

          You could use QJsonValue::isObject() or QJsonValue::isArray() to ensure value type before converting.
          But, as you know the right type, you only have to use the right functions ;)

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          J 1 Reply Last reply
          1
          • KroMignonK KroMignon

            @jenya7 said in Parsing JSON file:

            I get no errors but it doesn't work.

            Why should it work?
            You are trying to convert QJsonValue to QJsonArray, but according to your post it is a QJsonObject.

            You could use QJsonValue::isObject() or QJsonValue::isArray() to ensure value type before converting.
            But, as you know the right type, you only have to use the right functions ;)

            J Offline
            J Offline
            jenya7
            wrote on last edited by
            #17

            I'm lost from this point on. The best I could find

            QJsonValue temp_max = temperature.value("Maximum");
            

            Doesn't work.

            J.HilkJ KroMignonK 2 Replies Last reply
            0
            • J jenya7

              I'm lost from this point on. The best I could find

              QJsonValue temp_max = temperature.value("Maximum");
              

              Doesn't work.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #18

              @jenya7
              there is not much magic to it:

              "key": [] // Array
              "key":{} //Object
              "key":everything else //value
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • J jenya7

                I'm lost from this point on. The best I could find

                QJsonValue temp_max = temperature.value("Maximum");
                

                Doesn't work.

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #19

                @jenya7 said in Parsing JSON file:

                I'm lost from this point on. The best I could find

                   QJsonValue temp_max = temperature.value("Maximum");
                

                Doesn't work.

                What do you mean with does not work?

                This should work:

                for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
                {
                    // convert value to object
                    QJsonObject day = dayValue.toObject();
                    // according to your post, temperature seems to be an object, not an array
                    QJsonObject temperature = day["Temperature"].toObject();
                    QJsonObject maxTemp = temperature["Maximum"].toObject();
                    QJsonObject minTemp = temperature["Minimum"].toObject();
                    if(!maxTemp.isEmpty())
                        qDebug() << "'MaxTemp is" << maxTemp["Value"].toDouble() << maxTemp["Unit"].toString();
                    if(!minTemp.isEmpty())
                        qDebug() << "'MinTemp is" << minTemp["Value"].toDouble() << minTemp["Unit"].toString();   
                }
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                J 1 Reply Last reply
                4
                • J jenya7

                  @KroMignon said in Parsing JSON file:

                  @jenya7 said in Parsing JSON file:

                  I still don't know how to do it. :)

                  Yes, Qt documentation about JSON is a little bit short (https://doc.qt.io/qt-5/json.html).
                  You can iterate through the array:

                  
                  for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
                  {
                      // convert value to object
                      QJsonObject day = dayValue.toObject();
                      // according to your post, temperature seems to be an object, not an array
                      QJsonObject temperature = day["Temperature"].toObject();
                  
                  }
                  

                  Thank you. It works. And how to get objects inside the "Temperature"?
                  "Temperature"->"Maximum"->"Value"
                  "Temperature"->"Minimum"->"Value"
                  I wonder why I can't access it like this

                  QJsonArray temp = daily[0].toArray();
                  

                  I get no errors but it doesn't work.

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

                  @jenya7
                  As @KroMignon says.

                  While you are developing, and not understanding how your JSON is parsed/what objects/arrays etc. it is composed of, make use of QJsonValue::Type QJsonValue::type() const and enum QJsonValue::Type. If you print this all the time you would know how the JSON is being parsed, e.g.

                  qDebug() << jsonObject["DailyForecasts"].type() << day["Temperature"].type() << temperature["Maximum"].type()
                  

                  etc. That's how you know whether you can go toObject/toArray/toString() etc.

                  1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @jenya7 said in Parsing JSON file:

                    I'm lost from this point on. The best I could find

                       QJsonValue temp_max = temperature.value("Maximum");
                    

                    Doesn't work.

                    What do you mean with does not work?

                    This should work:

                    for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
                    {
                        // convert value to object
                        QJsonObject day = dayValue.toObject();
                        // according to your post, temperature seems to be an object, not an array
                        QJsonObject temperature = day["Temperature"].toObject();
                        QJsonObject maxTemp = temperature["Maximum"].toObject();
                        QJsonObject minTemp = temperature["Minimum"].toObject();
                        if(!maxTemp.isEmpty())
                            qDebug() << "'MaxTemp is" << maxTemp["Value"].toDouble() << maxTemp["Unit"].toString();
                        if(!minTemp.isEmpty())
                            qDebug() << "'MinTemp is" << minTemp["Value"].toDouble() << minTemp["Unit"].toString();   
                    }
                    
                    J Offline
                    J Offline
                    jenya7
                    wrote on last edited by
                    #21

                    @KroMignon said in Parsing JSON file:

                    @jenya7 said in Parsing JSON file:

                    I'm lost from this point on. The best I could find

                       QJsonValue temp_max = temperature.value("Maximum");
                    

                    Doesn't work.

                    What do you mean with does not work?

                    This should work:

                    for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
                    {
                        // convert value to object
                        QJsonObject day = dayValue.toObject();
                        // according to your post, temperature seems to be an object, not an array
                        QJsonObject temperature = day["Temperature"].toObject();
                        QJsonObject maxTemp = temperature["Maximum"].toObject();
                        QJsonObject minTemp = temperature["Minimum"].toObject();
                        if(!maxTemp.isEmpty())
                            qDebug() << "'MaxTemp is" << maxTemp["Value"].toDouble() << maxTemp["Unit"].toString();
                        if(!minTemp.isEmpty())
                            qDebug() << "'MinTemp is" << minTemp["Value"].toDouble() << minTemp["Unit"].toString();   
                    }
                    

                    It works. Thank you.

                    KroMignonK 1 Reply Last reply
                    0
                    • J jenya7

                      @KroMignon said in Parsing JSON file:

                      @jenya7 said in Parsing JSON file:

                      I'm lost from this point on. The best I could find

                         QJsonValue temp_max = temperature.value("Maximum");
                      

                      Doesn't work.

                      What do you mean with does not work?

                      This should work:

                      for(const auto & dayValue : jsonObject["DailyForecasts"].toArray())
                      {
                          // convert value to object
                          QJsonObject day = dayValue.toObject();
                          // according to your post, temperature seems to be an object, not an array
                          QJsonObject temperature = day["Temperature"].toObject();
                          QJsonObject maxTemp = temperature["Maximum"].toObject();
                          QJsonObject minTemp = temperature["Minimum"].toObject();
                          if(!maxTemp.isEmpty())
                              qDebug() << "'MaxTemp is" << maxTemp["Value"].toDouble() << maxTemp["Unit"].toString();
                          if(!minTemp.isEmpty())
                              qDebug() << "'MinTemp is" << minTemp["Value"].toDouble() << minTemp["Unit"].toString();   
                      }
                      

                      It works. Thank you.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #22

                      @jenya7 said in Parsing JSON file:

                      It works. Thank you.

                      But you did not reply to my question... What exactly did not work with your code?
                      AFAIK, there is no difference between temperature.value("Maximum") and temperature["Maximum"].

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      J 1 Reply Last reply
                      1
                      • KroMignonK KroMignon

                        @jenya7 said in Parsing JSON file:

                        It works. Thank you.

                        But you did not reply to my question... What exactly did not work with your code?
                        AFAIK, there is no difference between temperature.value("Maximum") and temperature["Maximum"].

                        J Offline
                        J Offline
                        jenya7
                        wrote on last edited by
                        #23

                        @KroMignon said in Parsing JSON file:

                        @jenya7 said in Parsing JSON file:

                        It works. Thank you.

                        But you did not reply to my question... What exactly did not work with your code?
                        AFAIK, there is no difference between temperature.value("Maximum") and temperature["Maximum"].

                        Yes. It should be the same. Probably I didn't cast it to the right data type.

                        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