How to process json array with QJsonxxx?
-
Assuming that
jsonStr
is aQString
variable whose value is[{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}]
.How can I process it with
QJsonxxx
class asjsonStr
being its input?I have a workaround as the following code snippet shows:
//_groupServiceInfoMap is a map of QMap<int, GroupServiceInfo>; // the definition of GroupServiceInfo is omitted here; QString jsonStr2 = jsonStr.mid(1, jsonStr.length() - 2); foreach (QString item, jsonStr2.split("},{")) { GroupServiceInfo info; item.remove("{"); item.remove("}"); item = "{" + item + "}"; QJsonParseError json_error; QJsonDocument doucment = QJsonDocument::fromJson(item.toUtf8(), &json_error); if (json_error.error == QJsonParseError::NoError) { if (!(doucment.isNull() || doucment.isEmpty())) { QVariantMap m = doucment.toVariant().toMap(); if (m.contains("groupId")) { info.id = m["groupId"].toInt(); } if (m.contains("description")) { info.desc = m["description"].toString(); } if (m.contains("name")) { info.name = m["name"].toString(); } _groupServiceInfoMap.insert(info.id, info); } } }
-
Here's an example of how to handle a JSON array https://github.com/sierdzio/stimeline/blob/master/src/stagsmodel.cpp#L42
-
Maybe I'm being stupid, can't you just use
QJsonArray
?const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**"); const QJsonDocument doucment = QJsonDocument::fromJson(jsonString); Q_ASSERT(doucment.isArray()); const QJsonArray documentArray = doucment.array(); for(const QJsonValue& jVal : documentArray){ qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString(); }
-
@sierdzio Thx. My final solution is as follow:
//_groupServiceInfoMap is a map of QMap<int, GroupServiceInfo>; // the definition of GroupServiceInfo is omitted here; const QJsonDocument doucment = QJsonDocument::fromJson(jsonStr.toUtf8()); Q_ASSERT(doucment.isArray()); const QJsonArray documentArray = doucment.array(); GroupServiceInfo info; for (const QJsonValue &i : documentArray) { const QJsonObject obj(i.toObject()); QVariantMap m = obj.toVariantMap(); if (m.contains("groupId")) { info.id = m["groupId"].toInt(); } if (m.contains("description")) { info.desc = m["description"].toString(); } if (m.contains("name")) { info.name = m["name"].toString(); } _groupServiceInfoMap.insert(info.id, info); }
-
Maybe I'm being stupid, can't you just use
QJsonArray
?const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**"); const QJsonDocument doucment = QJsonDocument::fromJson(jsonString); Q_ASSERT(doucment.isArray()); const QJsonArray documentArray = doucment.array(); for(const QJsonValue& jVal : documentArray){ qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString(); }
jVal["groupId"]
doesn't compile.@VRonin said in How to process json array with QJsonxxx?:
Maybe I'm being stupid, can't you just use
QJsonArray
?const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**"); const QJsonDocument doucment = QJsonDocument::fromJson(jsonString); Q_ASSERT(doucment.isArray()); const QJsonArray documentArray = doucment.array(); for(const QJsonValue& jVal : documentArray){ qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString(); }
-
jVal["groupId"]
doesn't compile.@VRonin said in How to process json array with QJsonxxx?:
Maybe I'm being stupid, can't you just use
QJsonArray
?const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**"); const QJsonDocument doucment = QJsonDocument::fromJson(jsonString); Q_ASSERT(doucment.isArray()); const QJsonArray documentArray = doucment.array(); for(const QJsonValue& jVal : documentArray){ qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString(); }