I have exactly following json data as follows:
[
{
"id":"01323",
"name":"Json Roy",
"contacts":[
"CONTACT1=+917673267299",
"CONTACT2=+917673267292",
"CONTACT3=+917673267293",
"CONTACT4=+917673267294",
]
}
]
I want to parse above jsonData data and extract contacts of that data.
QJsonParseError jerror;
QJsonDocument jsonData = QJsonDocument::fromJson(jsonData.c_str(),&jerror);
QJsonArray jsonArray = jsonData.array();
QJsonObject jsonObject = jsonData.object();
foreach (const QJsonValue & value, jsonArray){
string contact=jsonObject["contacts"].toString().toUtf8().constData();
}
can anybody suggest me how can i accomplish this with same above library?
I removed latest comma in the contacts list.
Your mistake is treating
QJsonValueas you want butQJsonValueis something like a wrapper so you should convert it to appropriate object ( array, object, string etc. ).jsonDatais not an object sojsonData.object()doesn't give you what you want.Here is the code, it could be the starting point for you.
Output :
CONTACT1=+917673267299 CONTACT2=+917673267292 CONTACT3=+917673267293 CONTACT4=+917673267294