0

When I use QJsonObject.insert() and pass a QJsonObject (which calls remove() before) as a parameter, it doesn't work well. I think it is more clear to post the code here:

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;

Like this ,the output will be wrong:

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":""}}}) )

and if I delete the line use 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;

this time it went well:

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":""}}}) )

Do you have any idea why?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57

0 Answers0