我想在QT中解析JSON。 JSON看起来像这样:
{
"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"
}
我使用以下代码:
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();
}
所以我得到了" tuc"的变体列表。阵列。一般来说,我希望用键"短语"来获取所有对象。和它们的值是这个数组的内容。有没有办法解决这个问题?或者这不适用于QVariantList?
答案 0 :(得分:0)
解决方案可能是
QVariantList phrases;
Q_FOREACH (const QVariant &v, phraseList) {
QVariantMap m = v.toMap();
if (m.contains("phrase")) {
phrases << m["phrase"].toMap();
}
}