Reputation: 471
I want to parse a JSON in QT. The JSON looks like this:
{
"result": "ok",
"phrase": "katze",
"tuc": [
{
"meaningId": -6468009888908805000,
"meanings": [
{
"text": "common name for animals",
"language": "eng"
}
],
"phrase": {
"text": "cats",
"language": "eng"
}
},
{
"meaningId": -1913936533709497000,
"phrase": {
"text": "felis catus",
"language": "eng"
}
},
{
"meaningId": 8369732998154311000,
"phrase": {
"text": "jack",
"language": "eng"
}
}
],
"from": "deu"
}
And I use following code:
void Slovari::fileIsReady( QNetworkReply * reply)
{
QByteArray rawData = reply->readAll();
QJsonDocument doc(QJsonDocument::fromJson(rawData));
QJsonObject jsonObject = doc.object();
QVariantMap mainmap = jsonObject.toVariantMap();
QVariantList phraseList = mainmap["tuc"].toList();
}
So I get a Variant List of the "tuc" array. Generally, I want to get all Object with the key "phrase" and their values which are the contents of this array. Is there some way to get it out of this? Or does this not work with a QVariantList?
Upvotes: 0
Views: 1693
Reputation: 1860
A solution could be
QVariantList phrases;
Q_FOREACH (const QVariant &v, phraseList) {
QVariantMap m = v.toMap();
if (m.contains("phrase")) {
phrases << m["phrase"].toMap();
}
}
Upvotes: 1