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
Forum Updated to NodeBB v4.3 + New Features

Parsing JSON file

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 2.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.
  • C Christian Ehrlicher
    3 Jan 2022, 09:14

    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 3 Jan 2022, 09:23 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. :)

    K 1 Reply Last reply 3 Jan 2022, 09:32
    0
    • J jenya7
      3 Jan 2022, 09:23

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

      K Offline
      K Offline
      KroMignon
      wrote on 3 Jan 2022, 09:32 last edited by KroMignon 1 Mar 2022, 09:33
      #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 3 Jan 2022, 10:08
      1
      • K KroMignon
        3 Jan 2022, 09:32

        @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 3 Jan 2022, 10:08 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.

        K J 2 Replies Last reply 3 Jan 2022, 10:13
        0
        • J jenya7
          3 Jan 2022, 10:08

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

          K Offline
          K Offline
          KroMignon
          wrote on 3 Jan 2022, 10:13 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 3 Jan 2022, 12:27
          1
          • K KroMignon
            3 Jan 2022, 10:13

            @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 3 Jan 2022, 12:27 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 K 2 Replies Last reply 3 Jan 2022, 12:40
            0
            • J jenya7
              3 Jan 2022, 12:27

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

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

              Doesn't work.

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 3 Jan 2022, 12:40 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
                3 Jan 2022, 12:27

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

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

                Doesn't work.

                K Offline
                K Offline
                KroMignon
                wrote on 3 Jan 2022, 13:32 last edited by KroMignon 1 Mar 2022, 13:32
                #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 3 Jan 2022, 15:01
                4
                • J jenya7
                  3 Jan 2022, 10:08

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Jan 2022, 14:14 last edited by JonB 1 Mar 2022, 14:16
                  #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
                  • K KroMignon
                    3 Jan 2022, 13:32

                    @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 3 Jan 2022, 15:01 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.

                    K 1 Reply Last reply 3 Jan 2022, 15:18
                    0
                    • J jenya7
                      3 Jan 2022, 15:01

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

                      K Offline
                      K Offline
                      KroMignon
                      wrote on 3 Jan 2022, 15:18 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 3 Jan 2022, 15:23
                      1
                      • K KroMignon
                        3 Jan 2022, 15:18

                        @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 3 Jan 2022, 15:23 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

                        22/23

                        3 Jan 2022, 15:18

                        • Login

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