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 3.0k 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.
  • 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