0

我对 QVariantMAP/List 和参考有点迷茫。

我用 QJson 加载一个 json 并将其转换为 QVariantMAP。currentJSON["tests"] 是一个 QVariantList

我希望浏览 currentJSON["tests"] 并更新 item["label"] 的值。第一个循环尝试更新值,第二个循环显示它。不幸的是,值显示不是更新的值。我想这是一个复制/参考问题,但我不知道如何解决它。

QVariantMap currentJSON = jObject.toVariantMap(); //jobject is the json
QVariantList l = qvariant_cast<QVariantList>(currentJSON["tests"]);
for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            test["label"].setValue(QVariant("AAAAAAAAAAAAAAAAAAA"));
}

l = qvariant_cast<QVariantList>(currentJSON["tests"]);
for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            //the value print is not AAAAAAAAAAAAAAAAAAA
            qDebug() << test["label"].toString();
}

如果你能帮助我,谢谢。

4

1 回答 1

0

好的,在 Amartel 的帮助下,我找到了这个解决方案:

 QVariantList l = qvariant_cast<QVariantList>(currentJSON["tests"]);
 for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            //qDebug() << hehe->toMap();
            QVariantMap test = hehe->toMap();
            test["label"].setValue(QVariant("AAAAAAAAAAAAAAAAAAA"));
            *hehe = test;
 }
 currentJSON["tests"] = l;

 l = qvariant_cast<QVariantList>(currentJSON["tests"]);
 for (QVariantList::iterator hehe = l.begin(); hehe != l.end(); hehe++) {
            QVariantMap test = hehe->toMap();
            qDebug() <<"y " << test["label"].toString();
 }

我添加了:*hehe = test; 当前JSON[“测试”] = l;

但是如果我有很多嵌套的列表,这有点复杂。有没有办法使用 reference 而不是 copy ?

于 2015-07-27T11:50:05.897 回答