I have QByteArray,contains this JSON
{"response":
{"count":2,
"items":[
{"name":"somename","key":1"},
{"name":"somename","key":1"}
]}}
Need to parse and get the required data:
QJsonDocument itemDoc = QJsonDocument::fromJson(answer);
QJsonObject itemObject = itemDoc.object();
qDebug()<<itemObject;
QJsonArray itemArray = itemObject["response"].toArray();
qDebug()<<itemArray;
First debug displays the contents of all QByteArray, recorded in itemObject, second debug does not display anything.
Must i parse this otherwise,or why this method does not work?
You either need to know the format, or work it out by asking the object about its type. This is why QJsonValue has functions such as isArray, toArray, isBool, toBool, etc.
If you know the format, you can do something like this: -
If you don't know the format, you'll have to ask each QJsonObject of its type and react accordingly. It's a good idea to check the type of a QJsonValue before converting it to its rightful object such as array, int etc.