当我使用QJsonObject.insert()
并传递一个QJsonObject(之前调用remove())作为参数时,它不能很好地工作。我认为在这里发布代码更为明确:
QJsonObject mainObj = myProJsonDoc.object();
QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
modulesObj.remove(QString::number(tempItem->moduleID));
qDebug()<<modulesObj;
qDebug()<<mainObj;
mainObj.insert(QLatin1String("modules"),modulesObj);
qDebug()<<mainObj<<(QJsonValue)modulesObj;
像这样,输出将是错误的:
QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}})
QJsonObject({})
QJsonObject({"modules":{"":null}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}}}) )
如果删除该行,请使用remove():
QJsonObject mainObj = myProJsonDoc.object();
QJsonObject modulesObj = mainObj.take(QLatin1String("modules")).toObject();
//modulesObj.remove(QString::number(tempItem->moduleID));
qDebug()<<modulesObj;
qDebug()<<mainObj;
mainObj.insert(QLatin1String("modules"),modulesObj);
qDebug()<<mainObj<<(QJsonValue)modulesObj;
这次进展顺利:
QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}})
QJsonObject({})
QJsonObject({"modules":{"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}}) QJsonValue(object, QJsonObject({"0":{"info":"balabala..","input":{"0":""},"name":"test1","output":{"0":""}},"1":{"info":"balabala..","input":{"0":""},"name":"","output":{"0":""}}}) )
你知道为什么吗?