更新 QJson 数组中的值并写回 Qt 中的 Json 文件

Update value in QJsonArray and write back to Json file in Qt

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

Json 文件示例

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

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

这是写函数。我尝试的是将 Json 文件解析为 QJson 数组,然后使用函数 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();
}

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

#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();
}