QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
QJsonValue value = jsonObject.value("enclosures");
QJsonArray array = value.toArray();
This code should work fine.
qDebug() << array;
qDebug() << array;
To copy to clipboard, switch view to plain text mode
gives the output as expected:
QJsonArray([{"data":{"enclosure_id":"0","enclosure_model":"MD1220 ","no_of_fans":"4","no_of_power_supplies":"2","no_of_slots":"24","product_revision_level":"1.01","temp_sensor_count":"4","vendor":"DELL "}}])
QJsonArray([{"data":{"enclosure_id":"0","enclosure_model":"MD1220 ","no_of_fans":"4","no_of_power_supplies":"2","no_of_slots":"24","product_revision_level":"1.01","temp_sensor_count":"4","vendor":"DELL "}}])
To copy to clipboard, switch view to plain text mode
Make sure your file/json is not corrupt. You can easily do that by adding a qDebug() statement after each line and see what you have then..
Edit:
Apparently I see there is a problem with how you read from this array in your first post:
Working version:
foreach (const QJsonValue & val, array)
{
QJsonObject dataObj = val.toObject().value("data").toObject();
qDebug() << dataObj.value("enclosure_id").toString();
}
foreach (const QJsonValue & val, array)
{
QJsonObject dataObj = val.toObject().value("data").toObject();
qDebug() << dataObj.value("enclosure_id").toString();
}
To copy to clipboard, switch view to plain text mode
PS: If you want to store enclosure_id as integer remove the double quotes.
Bookmarks