Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how can i modify the value of the Qjason array?
QtWS25 Last Chance

how can i modify the value of the Qjason array?

Scheduled Pinned Locked Moved Unsolved General and Desktop
jsonqjsonarray
4 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CupofCoffee
    wrote on 27 Jan 2023, 10:06 last edited by
    #1

    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?

    C J 2 Replies Last reply 27 Jan 2023, 10:44
    0
    • C CupofCoffee
      27 Jan 2023, 10:06

      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?

      C Offline
      C Offline
      CupofCoffee
      wrote on 27 Jan 2023, 10:44 last edited by
      #2

      @CupofCoffee
      prac.gif

      here is my program

      1 Reply Last reply
      0
      • Juan de DironneJ Offline
        Juan de DironneJ Offline
        Juan de Dironne
        wrote on 27 Jan 2023, 11:03 last edited by
        #3

        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
        }
        
        1 Reply Last reply
        1
        • C CupofCoffee
          27 Jan 2023, 10:06

          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?

          J Offline
          J Offline
          JonB
          wrote on 27 Jan 2023, 12:30 last edited by
          #4

          @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 the QJsonDocument. The same with the QJsonObject 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.

          1 Reply Last reply
          3

          1/4

          27 Jan 2023, 10:06

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved