Parse nested JSON with Qt5
-
Hello,
I am sending a GET request to openweathermap and store the JSON response in a QByteArray.
(This string is stored in QByteArray)Unfortunately I do not know how to access the nested elements in "weather".
This is the code I got:QByteArray val = reply->readAll(); QJsonDocument JsonDoc = QJsonDocument::fromJson(val); QJsonObject JsonObj = JsonDoc.object(); QJsonValue value = JsonObj.value(QString("weather")); qWarning() << value;
Console Output
QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
How can I access the nested elements in the QJsonArray?
Kind Regards
-
Thank you, but when I try to use the toArray method, I get the following error:
calling 'toarray' with incomplete return type 'qjsonarray'
@ven1ceBeach said in Parse nested JSON with Qt5:
calling 'toarray' with incomplete return type 'qjsonarray'
I very much doubt that is a copy & paste of the actual error message. It really does help if you copy error messages, not type in your own characters....
From the sound of the message, you have not done the necessary
#include
(s). Where do you have the required#include <QJsonArray>
? -
Hello,
I am sending a GET request to openweathermap and store the JSON response in a QByteArray.
(This string is stored in QByteArray)Unfortunately I do not know how to access the nested elements in "weather".
This is the code I got:QByteArray val = reply->readAll(); QJsonDocument JsonDoc = QJsonDocument::fromJson(val); QJsonObject JsonObj = JsonDoc.object(); QJsonValue value = JsonObj.value(QString("weather")); qWarning() << value;
Console Output
QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
How can I access the nested elements in the QJsonArray?
Kind Regards
@ven1ceBeach Well, you access each element of the array using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d and then access the content of each element depending on it's type. In your case you have objects as elements in your array.
-
Hello,
I am sending a GET request to openweathermap and store the JSON response in a QByteArray.
(This string is stored in QByteArray)Unfortunately I do not know how to access the nested elements in "weather".
This is the code I got:QByteArray val = reply->readAll(); QJsonDocument JsonDoc = QJsonDocument::fromJson(val); QJsonObject JsonObj = JsonDoc.object(); QJsonValue value = JsonObj.value(QString("weather")); qWarning() << value;
Console Output
QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
How can I access the nested elements in the QJsonArray?
Kind Regards
@ven1ceBeach said in Parse nested JSON with Qt5:
How can I access the nested elements in the QJsonArray?
You got all infos you need here!
I think you want to get access to first item of the array:QByteArray val = reply->readAll(); const auto jsonRoot = QJsonDocument::fromJson(val).object(); const auto weather = jsonRoot["weather"].toArray().first().toObject();
-
@ven1ceBeach said in Parse nested JSON with Qt5:
How can I access the nested elements in the QJsonArray?
You got all infos you need here!
I think you want to get access to first item of the array:QByteArray val = reply->readAll(); const auto jsonRoot = QJsonDocument::fromJson(val).object(); const auto weather = jsonRoot["weather"].toArray().first().toObject();
Thank you, but when I try to use the toArray method, I get the following error:
calling 'toarray' with incomplete return type 'qjsonarray'
-
Please post the correct error message instead typing it out of your memory.
If you want to use a class (in this case QJsonArray) you have to include the appropriate Header so this class is known to the compiler.
-
Thank you, but when I try to use the toArray method, I get the following error:
calling 'toarray' with incomplete return type 'qjsonarray'
@ven1ceBeach said in Parse nested JSON with Qt5:
calling 'toarray' with incomplete return type 'qjsonarray'
I very much doubt that is a copy & paste of the actual error message. It really does help if you copy error messages, not type in your own characters....
From the sound of the message, you have not done the necessary
#include
(s). Where do you have the required#include <QJsonArray>
?