Parsing JSON file
-
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 childQJsonObject data0 = jsonObject["DailyForecasts"].toObject (); QJsonObject data1 = data0 ["0"].toObject ();
I get an empty object.
-
DailyForcasts is an Array, not an object so use toArray()
-
@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> -
Please show your json instead some debugger output. Also please post how you try to parse the array.
-
@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.
-
@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 -
@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 outputThat'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>.
-
-
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.
-
@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 [?].?
-
@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. :)
-
@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(); }
-
@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 thisQJsonArray temp = daily[0].toArray();
I get no errors but it doesn't work.
-
@jenya7 said in Parsing JSON file:
I get no errors but it doesn't work.
Why should it work?
You are trying to convertQJsonValue
toQJsonArray
, but according to your post it is aQJsonObject
.You could use
QJsonValue::isObject()
orQJsonValue::isArray()
to ensure value type before converting.
But, as you know the right type, you only have to use the right functions ;) -
@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(); }
-
@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.