简体   繁体   中英

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

I have a Json file to read and display on UI. Reading is fine but when I tried to update value wheelbase, code runs with no errors but it does not update the Json

Json file example

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

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

This is the write function. What I have tried is to parse the Json file to the QJsonArray, then iterates and map with the function id parameter in order to update the wheelbase value.

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

You should place rootObject["$value"] = valuesArray; after the valuesArray.append(obj); . In your example, when append obj to valuesArray, you just update valuesArray, have not update 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM