how can i modify the value of the Qjason array?
-
hi!
i'm learning Qt and Json.
i've been searching a lot but, i'm still can't modifying json array value.
i'd appreciate your help.-full json file if you want-
full json file (easyupload link)
https://easyupload.io/jhmklg
summary of Json file
{ "info": { "LastSaveTime": "2023-01-25T07:33:44.246Z", "TrendInfo": [ { "trendID": 1, "trendName": "trendA", "trendItems": "apple, banana", "trendTerm": 2 }, { "trendID": 2, "trendName": "trendB", "trendItems": "bike, motobike", "trendTerm": 3 }, { // .....(trendID 8 is last) } ] }, "TradingAreaInfo": [ { "AreaId": 1, "AreaName": "areaA", "TrendStatus": [ /*<< I want to update all ["LastTrendTime"] value of this array.*/ { "trendid": 1, "LastTrendTime": "22:00" // <<i want modify this value }, { "trendid": 2, "LastTrendTime": "21:00" // <<i want modify this value } { //trendid 8 is last. }, ] }, { "AreaId": 2, "AreaName": "areaB", "TrendStatus": [ { "trendid": 1, "LastTrendTime": "22:00" }, { //trendid 8 is last. }, ] }, { //AreaId 14 is last.. } ] }
and code.
void TrendInUWO::ChangeValueofTable(QTableView* Table, int areaID) { /*get model*/ QStandardItemModel* model = (QStandardItemModel*)Table->model(); int areaIDforArr = areaID - 1; /*get Json file*/ QJsonDocument Doc = loadJson(FilePath); rootObj = Doc.object(); QJsonObject InfoObj = rootObj["info"].toObject(); /*change value at 'info->LastSaveTime' ,, it's works*/ InfoObj.insert("LastSaveTime",QJsonValue::fromVariant(QDateTime::currentDateTime().toString(Qt::ISODate))); rootObj.insert("info",InfoObj); TradingAreaInfoArr = rootObj["TradingAreaInfo"].toArray(); /* get TradingAreaInfo Array[index] Object*/ QJsonObject TAIObj = TradingAreaInfoArr[areaIDforArr].toObject(); /*and get TrendStatus array at TAIObj */ QJsonArray TrendstatusArr = TAIObj.value("TrendStatus").toArray(); /* and looping as much as TrendStatusArray size*/ for (int i = 0; i < TrendstatusArr.size(); ++i) { /* get value of QTableView's model*/ QString timestr = model->data(model->index(i, 1)).toString(); /* and get TrendStatus Array's index Object for modify */ QJsonObject ArrelementObj =TrendstatusArr[i].toObject(); /* modify and put it back*/ ArrelementObj["LastTrendTime"] = timestr; TrendstatusArr[i] = ArrelementObj; } /* Appy modified QJsonObject after modify is completed*/ TAIObj["TrendStatus"] = TrendstatusArr; TradingAreaInfoArr[areaIDforArr] = TAIObj; //not working.. //TradingAreaInfoArr.insert(areaIDforArr,TAIObj); // not working. //rootObj["TradingAreaInfo"] = TAIObj; //it works, but delete all other TradingAreaInfo Array.. //save file. Doc.setObject(rootObj); saveJson(Doc, FilePath); SettingGrid(); //widget refresh }
how can i modify
["LastTrendTime"] value of TrendStatus[all index] in TradingAreaInfo[0] array? -
here is my program
-
This is not the total solution to your problem, but it may help you understand how to iterate through a JSON containing arrays :)
#include<QJsonDocument> #include<QJsonObject> #include<QJsonArray> #include<QFile> int main (int nbArg, char* listArg[]) { QFile file("my_another_file.json"); file.open(QIODevice::ReadOnly); QByteArray rawData = file.readAll(); file.close(); QJsonDocument docJson = QJsonDocument::fromJson(rawData); QJsonObject myRoot = docJson.object(); QJsonArray TradingAreaInfoList = myRoot.value("TradingAreaInfo").toArray(); // For Each Trading Area Info for(int i=0;i<TradingAreaInfoList.size();i++) { // Extraction Information QJsonObject TradingAreaInfo = TradingAreaInfoList[i].toObject(); QJsonArray TrendStatusList = TradingAreaInfo.value("TrendStatus").toArray(); // For Each Trend Status for(int j=0;j<TrendStatusList.size();j++) { // Extraction Information QJsonObject TrendStatus = TrendStatusList[j].toObject(); qDebug() << TrendStatus["LastTrendTime"]; } // End - For Each Trend Status } // End - For Each Trading Area Info }
-
@CupofCoffee said in how can i modify the value of the Qjason array?:
TradingAreaInfoArr = rootObj["TradingAreaInfo"].toArray();
My understanding is that this returns a copy of the
QJsonArray
. You can modify that copy (update values, append new ones), but that does not alter the array in theQJsonDocument
. The same with theQJsonObject InfoObj = rootObj["info"].toObject();
The objects/arrays in these are read-only.You have to copy them out as above, alter them, and then replace the current objects/arrays in the document, or create a new one, in order to produce a new document with the updates which can be saved.