我有一個 Json 檔案可以在 UI 上讀取和顯示。閱讀很好,但是當我嘗試更新值軸距時,代碼運行沒有錯誤,但它不會更新 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();
}
uj5u.com熱心網友回復:
你應該把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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/313879.html
