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