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(); }
@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.
-
@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.
-
@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 betweentemperature.value("Maximum")
andtemperature["Maximum"]
.@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 betweentemperature.value("Maximum")
andtemperature["Maximum"]
.Yes. It should be the same. Probably I didn't cast it to the right data type.