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.7k 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.
  • J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #1

    I get a JSON file and parse

    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();
    

    At this point I see

    QJsonObject
    
    QString
    	jsonObject <2 items>
        QJsonObject  "DailyForecasts" <1 items>
            QJsonValue (Array)  [0]	<15 items>
                QJsonValue (Object)  "AirAndPollen" <6 items>
                QJsonValue (Array)  "Date" "2022-01-02T07:00:00+02:00"
    	    QJsonValue (String)  "Day" <23 items>
    	    QJsonValue (Object)  "DegreeDaySummary"	<2 items>
    	    QJsonValue (Object)  "EpochDate"	1641099600.0
    	    QJsonValue (Number)  "HoursOfSun"  9.3	
                QJsonValue (Number)  "Link"  "http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us"	
                QJsonValue (String)  "MobileLink" "http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us"
    	    QJsonValue (String)  "Moon"  <6 items>
    	    QJsonValue (Object)  "Night" <25 items>	
                QJsonValue (Object)  "RealFeelTemperature" <2 items>
    	    QJsonValue (Object)  "RealFeelTemperatureShade"	<2 items>
    	    QJsonValue (Object)  "Sources"  <1 items>
    	    QJsonValue (Array)   "Sun"  <4 items>	
                QJsonValue (Object)  "Temperature" <2 items>
    	       QJsonValue (Object)"Maximum" <3 items>
                        QJsonValue (Object)  "Unit" "F"	
                        QJsonValue (String)  "UnitType"	18
        	            QJsonValue (Number)  "Value"	67.0
    	    QJsonValue (Number)   "Minimum" <3 items>
        QJsonValue (Object)  "Headline"  <9 items>
        QJsonValue (Object)
    

    How can I get down to the value - "DailyForecasts" ->"Temperature"->"Maximum"->"Value"?
    I try the next child

    QJsonObject data0 = jsonObject["DailyForecasts"].toObject ();
    QJsonObject data1 =  data0 ["0"].toObject ();
    

    I get an empty object.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      DailyForcasts is an Array, not an object so use toArray()

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

      J 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        DailyForcasts is an Array, not an object so use toArray()

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

        @Christian-Ehrlicher said in Parsing JSON file:

        DailyForcasts is an Array, not an object so use toArray()

        QJsonArray daily = jsonObject["DailyForecasts"].toArray ();
        

        I get
        daily <at least 100000000 items>

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please show your json instead some debugger output. Also please post how you try to parse the array.

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

          J 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Please show your json instead some debugger output. Also please post how you try to parse the array.

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

            @Christian-Ehrlicher said in Parsing JSON file:

            Please show your json instead some debugger output. Also please post how you try to parse the array.

            it's quite large.

            {
            "Headline":
            {
                "EffectiveDate":"2022-01-02T13:00:00+02:00",
                "EffectiveEpochDate":1641121200,
                "Severity":4,
                "Text":"Expect rainy weather Sunday afternoon through Sunday evening",
                "Category":"rain",
                "EndDate":"2022-01-03T01:00:00+02:00",
                "EndEpochDate":1641164400,
                "MobileLink":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?lang=en-us",
                "Link":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?lang=en-us"
            },
            "DailyForecasts":
            [
            {
                "Date":"2021-12-30T07:00:00+02:00",
                "EpochDate":1640840400,
                "Sun":
            	{
            	    "Rise":"2021-12-30T06:41:00+02:00",
            	    "EpochRise":1640839260,
            	    "Set":"2021-12-30T16:45:00+02:00",
            	    "EpochSet":1640875500
            	},
            	"Moon":
            	{
            	    "Rise":"2021-12-30T02:43:00+02:00",
            	    "EpochRise":1640824980,
            	    "Set":"2021-12-30T13:40:00+02:00",
            	    "EpochSet":1640864400,
            	    "Phase":"WaningCrescent",
            	    "Age":26
            	},
            	"Temperature":
            	{
            	    "Minimum":
            	    {
            	        "Value":55.0,
            	        "Unit":"F",
            	        "UnitType":18
            	    },
            	    "Maximum":
            	    {
            	        "Value":68.0,
            	        "Unit":"F",
            	        "UnitType":18
            	    }
            	},
            	"RealFeelTemperature":
            	{
            	    "Minimum":
            	    {
            	    "Value":55.0,
            	    "Unit":"F",
            	    "UnitType":18,
            	    "Phrase":"Cool"
            	    },
            	    "Maximum":
            	    {
            	    "Value":68.0,
            	    "Unit":"F",
            	    "UnitType":18,
            	    "Phrase":"Pleasant"
            	    }
            	},
            	"RealFeelTemperatureShade":
            	{
            	    "Minimum":
            	    {
            	    "Value":55.0,
            	    "Unit":"F",
            	    "UnitType":18,
            	    "Phrase":"Cool"
            	    },
            	    "Maximum":
            	    {
            	    "Value":65.0,
            	    "Unit":"F",
            	    "UnitType":18,
            	    "Phrase":"Pleasant"
            	    }
            	},
            	"HoursOfSun":4.5,
            	"DegreeDaySummary":
            	{
            	    "Heating":
            	    {
            	    "Value":3.0,
            	    "Unit":"F",
            	    "UnitType":18
            	    },
            	    "Cooling":
            	    {
            	    "Value":0.0,
            	    "Unit":"F",
            	    "UnitType":18
            	    }
            	},
            	"AirAndPollen":
            	[
            	{
            	"Name":"AirQuality",
            	"Value":0,
            	"Category":"Good",
            	"CategoryValue":1,
            	"Type":"Ozone"
            	},
            	{
            	"Name":"Grass",
            	"Value":0,
            	"Category":"Low",
            	"CategoryValue":1
            	},
            	{
            	"Name":"Mold",
            	"Value":0,
            	"Category":"Low",
            	"CategoryValue":1
            	},
            	{
            	"Name":"Ragweed",
            	"Value":0,
            	"Category":"Low",
            	"CategoryValue":1
            	},
            	{
            	"Name":"Tree",
            	"Value":0,
            	"Category":"Low","CategoryValue":1
            	},
            	{
            	"Name":"UVIndex",
            	"Value":3,
            	"Category":"Moderate",
            	"CategoryValue":2
            	}
            	],
            	"Day":
            	{
            	"Icon":4,
            	"IconPhrase":"Intermittent clouds",
            	"HasPrecipitation":false,
            	"ShortPhrase":"Clouds and sun",
            	"LongPhrase":"Intervals of clouds and sunshine",
            	"PrecipitationProbability":25,
            	"ThunderstormProbability":0,
            	"RainProbability":25,
            	"SnowProbability":0,
            	"IceProbability":0,
            	"Wind":
            	{
            	"Speed":
            	{
            	"Value":6.9,
            	"Unit":"mi/h",
            	"UnitType":9
            	},
            	"Direction":
            	{
            	"Degrees":157,
            	"Localized":"SSE",
            	"English":"SSE"
            	}
            	},
            	"WindGust":
            	{
            	"Speed":
            	{
            	"Value":13.8,
            	"Unit":"mi/h",
            	"UnitType":9},
            	"Direction":
            	{
            	"Degrees":177,
            	"Localized":"S",
            	"English":"S"
            	}
            	},
            	"TotalLiquid":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1},
            	"Rain":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"Snow":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"Ice":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"HoursOfPrecipitation":0.0,
            	"HoursOfRain":0.0,
            	"HoursOfSnow":0.0,
            	"HoursOfIce":0.0,
            	"CloudCover":68,
            	"Evapotranspiration":
            	{
            	"Value":0.07,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"SolarIrradiance":
            	{"Value":416.3,
            	"Unit":"W/m²",
            	"UnitType":33
            	}
            	},
            	"Night":
            	{"Icon":7,
            	"IconPhrase":
            	"Cloudy",
            	"HasPrecipitation":false,
            	"ShortPhrase":"Considerable cloudiness",
            	"LongPhrase":"Considerable cloudiness",
            	"PrecipitationProbability":14,
            	"ThunderstormProbability":0,
            	"RainProbability":14,
            	"SnowProbability":0,
            	"IceProbability":0,
            	"Wind":
            	{
            	"Speed":
            	{
            	"Value":6.9,
            	"Unit":"mi/h",
            	"UnitType":9
            	},
            	"Direction":
            	{
            	"Degrees":131,
            	"Localized":"SE",
            	"English":"SE"
            	}
            	},
            	"WindGust":
            	{
            	"Speed":
            	{
            	"Value":16.1,
            	"Unit":"mi/h",
            	"UnitType":9},
            	"Direction":
            	{
            	"Degrees":117,
            	"Localized":"ESE",
            	"English":"ESE"
            	}
            	},
            	"TotalLiquid":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"Rain":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1},
            	"Snow":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"Ice":
            	{
            	"Value":0.0,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"HoursOfPrecipitation":0.0,
            	"HoursOfRain":0.0,
            	"HoursOfSnow":0.0,
            	"HoursOfIce":0.0,
            	"CloudCover":99,
            	"Evapotranspiration":
            	{
            	"Value":0.03,
            	"Unit":"in",
            	"UnitType":1
            	},
            	"SolarIrradiance":
            	{
            	"Value":0.0,
            	"Unit":"W/m²",
            	"UnitType":33
            	}
            	},
            	"Sources":
            	["AccuWeather"],
            	"MobileLink":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us",
            	"Link":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us"
            	}
            	]
            	}
            

            I just set a break point at daily to see what next nodes I get.

            JonBJ 1 Reply Last reply
            0
            • J jenya7

              @Christian-Ehrlicher said in Parsing JSON file:

              Please show your json instead some debugger output. Also please post how you try to parse the array.

              it's quite large.

              {
              "Headline":
              {
                  "EffectiveDate":"2022-01-02T13:00:00+02:00",
                  "EffectiveEpochDate":1641121200,
                  "Severity":4,
                  "Text":"Expect rainy weather Sunday afternoon through Sunday evening",
                  "Category":"rain",
                  "EndDate":"2022-01-03T01:00:00+02:00",
                  "EndEpochDate":1641164400,
                  "MobileLink":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?lang=en-us",
                  "Link":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?lang=en-us"
              },
              "DailyForecasts":
              [
              {
                  "Date":"2021-12-30T07:00:00+02:00",
                  "EpochDate":1640840400,
                  "Sun":
              	{
              	    "Rise":"2021-12-30T06:41:00+02:00",
              	    "EpochRise":1640839260,
              	    "Set":"2021-12-30T16:45:00+02:00",
              	    "EpochSet":1640875500
              	},
              	"Moon":
              	{
              	    "Rise":"2021-12-30T02:43:00+02:00",
              	    "EpochRise":1640824980,
              	    "Set":"2021-12-30T13:40:00+02:00",
              	    "EpochSet":1640864400,
              	    "Phase":"WaningCrescent",
              	    "Age":26
              	},
              	"Temperature":
              	{
              	    "Minimum":
              	    {
              	        "Value":55.0,
              	        "Unit":"F",
              	        "UnitType":18
              	    },
              	    "Maximum":
              	    {
              	        "Value":68.0,
              	        "Unit":"F",
              	        "UnitType":18
              	    }
              	},
              	"RealFeelTemperature":
              	{
              	    "Minimum":
              	    {
              	    "Value":55.0,
              	    "Unit":"F",
              	    "UnitType":18,
              	    "Phrase":"Cool"
              	    },
              	    "Maximum":
              	    {
              	    "Value":68.0,
              	    "Unit":"F",
              	    "UnitType":18,
              	    "Phrase":"Pleasant"
              	    }
              	},
              	"RealFeelTemperatureShade":
              	{
              	    "Minimum":
              	    {
              	    "Value":55.0,
              	    "Unit":"F",
              	    "UnitType":18,
              	    "Phrase":"Cool"
              	    },
              	    "Maximum":
              	    {
              	    "Value":65.0,
              	    "Unit":"F",
              	    "UnitType":18,
              	    "Phrase":"Pleasant"
              	    }
              	},
              	"HoursOfSun":4.5,
              	"DegreeDaySummary":
              	{
              	    "Heating":
              	    {
              	    "Value":3.0,
              	    "Unit":"F",
              	    "UnitType":18
              	    },
              	    "Cooling":
              	    {
              	    "Value":0.0,
              	    "Unit":"F",
              	    "UnitType":18
              	    }
              	},
              	"AirAndPollen":
              	[
              	{
              	"Name":"AirQuality",
              	"Value":0,
              	"Category":"Good",
              	"CategoryValue":1,
              	"Type":"Ozone"
              	},
              	{
              	"Name":"Grass",
              	"Value":0,
              	"Category":"Low",
              	"CategoryValue":1
              	},
              	{
              	"Name":"Mold",
              	"Value":0,
              	"Category":"Low",
              	"CategoryValue":1
              	},
              	{
              	"Name":"Ragweed",
              	"Value":0,
              	"Category":"Low",
              	"CategoryValue":1
              	},
              	{
              	"Name":"Tree",
              	"Value":0,
              	"Category":"Low","CategoryValue":1
              	},
              	{
              	"Name":"UVIndex",
              	"Value":3,
              	"Category":"Moderate",
              	"CategoryValue":2
              	}
              	],
              	"Day":
              	{
              	"Icon":4,
              	"IconPhrase":"Intermittent clouds",
              	"HasPrecipitation":false,
              	"ShortPhrase":"Clouds and sun",
              	"LongPhrase":"Intervals of clouds and sunshine",
              	"PrecipitationProbability":25,
              	"ThunderstormProbability":0,
              	"RainProbability":25,
              	"SnowProbability":0,
              	"IceProbability":0,
              	"Wind":
              	{
              	"Speed":
              	{
              	"Value":6.9,
              	"Unit":"mi/h",
              	"UnitType":9
              	},
              	"Direction":
              	{
              	"Degrees":157,
              	"Localized":"SSE",
              	"English":"SSE"
              	}
              	},
              	"WindGust":
              	{
              	"Speed":
              	{
              	"Value":13.8,
              	"Unit":"mi/h",
              	"UnitType":9},
              	"Direction":
              	{
              	"Degrees":177,
              	"Localized":"S",
              	"English":"S"
              	}
              	},
              	"TotalLiquid":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1},
              	"Rain":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"Snow":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"Ice":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"HoursOfPrecipitation":0.0,
              	"HoursOfRain":0.0,
              	"HoursOfSnow":0.0,
              	"HoursOfIce":0.0,
              	"CloudCover":68,
              	"Evapotranspiration":
              	{
              	"Value":0.07,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"SolarIrradiance":
              	{"Value":416.3,
              	"Unit":"W/m²",
              	"UnitType":33
              	}
              	},
              	"Night":
              	{"Icon":7,
              	"IconPhrase":
              	"Cloudy",
              	"HasPrecipitation":false,
              	"ShortPhrase":"Considerable cloudiness",
              	"LongPhrase":"Considerable cloudiness",
              	"PrecipitationProbability":14,
              	"ThunderstormProbability":0,
              	"RainProbability":14,
              	"SnowProbability":0,
              	"IceProbability":0,
              	"Wind":
              	{
              	"Speed":
              	{
              	"Value":6.9,
              	"Unit":"mi/h",
              	"UnitType":9
              	},
              	"Direction":
              	{
              	"Degrees":131,
              	"Localized":"SE",
              	"English":"SE"
              	}
              	},
              	"WindGust":
              	{
              	"Speed":
              	{
              	"Value":16.1,
              	"Unit":"mi/h",
              	"UnitType":9},
              	"Direction":
              	{
              	"Degrees":117,
              	"Localized":"ESE",
              	"English":"ESE"
              	}
              	},
              	"TotalLiquid":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"Rain":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1},
              	"Snow":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"Ice":
              	{
              	"Value":0.0,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"HoursOfPrecipitation":0.0,
              	"HoursOfRain":0.0,
              	"HoursOfSnow":0.0,
              	"HoursOfIce":0.0,
              	"CloudCover":99,
              	"Evapotranspiration":
              	{
              	"Value":0.03,
              	"Unit":"in",
              	"UnitType":1
              	},
              	"SolarIrradiance":
              	{
              	"Value":0.0,
              	"Unit":"W/m²",
              	"UnitType":33
              	}
              	},
              	"Sources":
              	["AccuWeather"],
              	"MobileLink":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us",
              	"Link":"http://www.accuweather.com/en/il/hadassim/212521/daily-weather-forecast/212521?day=1&lang=en-us"
              	}
              	]
              	}
              

              I just set a break point at daily to see what next nodes I get.

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

              @jenya7
              qDebug() << daily.count() instead of relying on debugger interpretation.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @jenya7 said in Parsing JSON file:

                I just set a break point

                When you don't show us your code - how should we help?
                Please show us how you try to parse the array, not some debugger output

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

                J 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @jenya7 said in Parsing JSON file:

                  I just set a break point

                  When you don't show us your code - how should we help?
                  Please show us how you try to parse the array, not some debugger output

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

                  @Christian-Ehrlicher said in Parsing JSON file:

                  @jenya7 said in Parsing JSON file:

                  I just set a break point

                  When you don't show us your code - how should we help?
                  Please show us how you try to parse the array, not some debugger output

                  That's all my code

                  QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                  QJsonObject jsonObject = jsonResponse.object();
                     
                  QJsonArray daily = jsonObject["[DailyForecasts"].toArray();
                  

                  in jsonObject I see all objects. daily <0 items>.

                  KroMignonK JonBJ 2 Replies Last reply
                  0
                  • J jenya7

                    @Christian-Ehrlicher said in Parsing JSON file:

                    @jenya7 said in Parsing JSON file:

                    I just set a break point

                    When you don't show us your code - how should we help?
                    Please show us how you try to parse the array, not some debugger output

                    That's all my code

                    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                    QJsonObject jsonObject = jsonResponse.object();
                       
                    QJsonArray daily = jsonObject["[DailyForecasts"].toArray();
                    

                    in jsonObject I see all objects. daily <0 items>.

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

                    @jenya7 said in Parsing JSON file:

                    QJsonArray daily = jsonObject["[DailyForecasts"].toArray();

                    I don't known if it is a copy past from your real code.
                    If it is the case there is a typo ==> "[DailyForecasts" should be "DailyForecasts"

                    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
                    • J jenya7

                      @Christian-Ehrlicher said in Parsing JSON file:

                      @jenya7 said in Parsing JSON file:

                      I just set a break point

                      When you don't show us your code - how should we help?
                      Please show us how you try to parse the array, not some debugger output

                      That's all my code

                      QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
                      QJsonObject jsonObject = jsonResponse.object();
                         
                      QJsonArray daily = jsonObject["[DailyForecasts"].toArray();
                      

                      in jsonObject I see all objects. daily <0 items>.

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

                      @jenya7 said in Parsing JSON file:

                      QJsonArray daily = jsonObject["[DailyForecasts"].toArray();

                      That's why it's so important you show your actual code....

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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.

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

                        J 1 Reply Last reply
                        1
                        • KroMignonK KroMignon

                          @jenya7 said in Parsing JSON file:

                          QJsonArray daily = jsonObject["[DailyForecasts"].toArray();

                          I don't known if it is a copy past from your real code.
                          If it is the case there is a typo ==> "[DailyForecasts" should be "DailyForecasts"

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

                          @KroMignon said in Parsing JSON file:

                          @jenya7 said in Parsing JSON file:

                          QJsonArray daily = jsonObject["[DailyForecasts"].toArray();

                          I don't known if it is a copy past from your real code.
                          If it is the case there is a typo ==> "[DailyForecasts" should be "DailyForecasts"

                          Yes. Sorry. Was my typo. Now it seems to be OK.
                          But how can I go further?

                           QJsonArray daily1 = jsonObject["DailyForecasts"].toArray();
                           QJsonArray temp = daily1 [?].?
                          
                          1 Reply Last reply
                          0
                          • 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 Offline
                                      J.HilkJ Offline
                                      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

                                          • Login

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