【问题标题】:Update value in QJsonArray and write back to Json file in Qt更新 QJsonArray 中的值并写回 Qt 中的 Json 文件
【发布时间】:2021-10-08 07:50:19
【问题描述】:

我有一个要在 UI 上读取和显示的 Json 文件。阅读很好,但是当我尝试更新值轴距时,代码运行没有错误,但它没有更新 Json

Json 文件示例

{
"$type": "SystemList",
    "$values": [
        {
            "chassicId": 1000,
            "wheelbase": 98

        },
        {
            "chassicId": 1001,
            "wheelbase": 102
        }
     ]
}

这是写函数。我尝试的是将Json文件解析为QJsonArray,然后迭代并使用函数id参数映射以更新轴距值。

void updateWheelbase(const int& id)
{
   
        updatedWheelbase = dialog.wheelbaseInput().toDouble();
        QFile file("json file path");
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QByteArray jsonData = file.readAll();
        file.close();

        QJsonDocument itemDoc = QJsonDocument::fromJson(jsonData);
        QJsonObject rootObject = itemDoc.object();

        QJsonArray valuesArray = rootObject.value("$values").toArray();

        //get a copy of the QJsonObject
        QJsonObject obj;
        for (auto v: valuesArray) {
            QJsonObject o = v.toObject();
            if (o.value("chassicId").toInt() == id) {
                obj = o;
                break;
            }
        }

        // modify the object
        obj["wheelbase"] = updatedWheelbase;
        int position = 0;

        // erase the old instance of the object
        for (auto it = valuesArray.begin(); it != valuesArray.end(); ++it) {
            QJsonObject obj = (*it).toObject();
            if (obj.value("chassicId").toInt() == id) {
                valuesArray.erase(it);

                //assign the array copy which has the object deleted
                rootObject["$value"] = valuesArray;
                break;
            }
            position++;
        }

        // add the modified QJsonObject
        valuesArray.append(obj);

        // replace the original array with the array containing our modified threshold object
        itemDoc.setObject(rootObject);

        file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
        file.write(itemDoc.toJson());
        file.close();
}

【问题讨论】:

    标签: c++ json qt qjson qjsonobject


    【解决方案1】:

    您应该将rootObject["$value"] = valuesArray; 放在valuesArray.append(obj); 之后。在您的示例中,当将 obj 附加到 valuesArray 时,您只是更新 valuesArray,没有更新 rootObject。

    #include <QtWidgets/QApplication>
    #include <QDebug>
    #include <QFile>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>
    
    void updateWheelbase()
    {
        int id = 1001;
        double updatedWheelbase = 1.1;
        QFile file("./Debug/a.json");
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QByteArray jsonData = file.readAll();
        file.close();
    
        QJsonDocument itemDoc = QJsonDocument::fromJson(jsonData);
        QJsonObject rootObject = itemDoc.object();
    
        QJsonArray valuesArray = rootObject.value("$values").toArray();
    
        //get a copy of the QJsonObject
        QJsonObject obj;
        for (auto v : valuesArray) {
            QJsonObject o = v.toObject();
            if (o.value("chassicId").toInt() == id) {
                obj = o;
                break;
            }
        }
    
        // modify the object
        obj["wheelbase"] = updatedWheelbase;
        int position = 0;
    
        // erase the old instance of the object
        for (auto it = valuesArray.begin(); it != valuesArray.end(); ++it) {
            QJsonObject obj = (*it).toObject();
            if (obj.value("chassicId").toInt() == id) {
                valuesArray.erase(it);
    
                //assign the array copy which has the object deleted
                rootObject["$values"] = valuesArray;
                break;
            }
            position++;
        }
    
        // add the modified QJsonObject
        valuesArray.append(obj);
        rootObject["$values"] = valuesArray;
        // replace the original array with the array containing our modified threshold object
        itemDoc.setObject(rootObject);
    
        file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
        file.write(itemDoc.toJson());
        file.close();
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        updateWheelbase();
        return a.exec();
    }
    

    【讨论】:

    • 是的,你是对的,我应该更新 rootObject。尝试了您的解决方案,但仍然无效。
    • 我刚查了一下,发现写入json文件时,QIODevice::write:设备没有打开,是不是我没有权限写文件?跨度>
    • 我试过你的代码,并改变了我所说的,它工作正常。如果您的不起作用,则可能是文件路径有问题。 @NeungChung
    • 你的json文件存放在哪里?
    • 我在答案中添加了我的代码,你可以比较一下。 @NeungChung
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多