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 to Write Data to json file From Qt
Forum Update on Tuesday, May 27th 2025

How to Write Data to json file From Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 718 Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Raji
    wrote on last edited by
    #1

    Hi i am New to Qt , I am Practicing How to Insert the data to json file .Unable to insert the data to devip
    Tried following way please help me .
    {
    "connection":{
    "type":"ETH10G",
    "param":{
    "devip":"192.168.0.166",
    "localip":"192.168.0.160",
    "stream_port":15560,
    "control_port":15561,
    "event_port":15562
    }
    }
    QFile file1("vlcfg_sim.json");
    file1.open(QIODevice::ReadOnly | QIODevice::Text);
    QJsonParseError JsonParseError1;
    QJsonDocument JsonDocument1 = QJsonDocument::fromJson(file1.readAll(), &JsonParseError1);
    file1.close();
    QJsonObject RootObject = JsonDocument1.object();
    QJsonObject connectionData = RootObject["connection"].toObject();
    QJsonObject param2Data = connectionData["param"].toObject();

    QJsonValueRef ref = RootObject.find("connection").value() ;
    QJsonObject m_addvalue = ref.toObject();
    m_addvalue.insert(key,name);//set the value you want to modify
    ref=m_addvalue; //assign the modified object to reference
    JsonDocument1.setObject(RootObject); // set to json document
    file1.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
    file1.write(JsonDocument1.toJson());
    file1.close();
    

    unable to write the data into param->devid, please can you any one help .

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Are you getting any errors? Or resulting file is wrong? Please provide more information.

      (Z(:^

      R 1 Reply Last reply
      2
      • sierdzioS sierdzio

        Are you getting any errors? Or resulting file is wrong? Please provide more information.

        R Offline
        R Offline
        Raji
        wrote on last edited by Raji
        #3

        @sierdzio Result file is wrong , i want to modify/insert the below data values of both at same time
        connection->type
        connection->param->devip

        JonBJ 1 Reply Last reply
        0
        • R Raji

          @sierdzio Result file is wrong , i want to modify/insert the below data values of both at same time
          connection->type
          connection->param->devip

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Raji
          You have already shown that you know how to get a reference to a value/object, so that you can make changes. param->devip is a keyed value inside param in just the same way that connection->param is a keyed object inside connection. You can either remove() and re-insert() your type & devip inside their parents, or you can use QJsonValueRef to overwrite the value already there. Then you save back when you have done both.

          R 1 Reply Last reply
          1
          • JonBJ JonB

            @Raji
            You have already shown that you know how to get a reference to a value/object, so that you can make changes. param->devip is a keyed value inside param in just the same way that connection->param is a keyed object inside connection. You can either remove() and re-insert() your type & devip inside their parents, or you can use QJsonValueRef to overwrite the value already there. Then you save back when you have done both.

            R Offline
            R Offline
            Raji
            wrote on last edited by
            #5

            @JonB : Thank you , but while writing to file param value saving into connection
            Ex: connection{
            type: " value"
            devip: " 113"
            param{
            }
            }
            getting like this, but i need devip inside param

            JonBJ 1 Reply Last reply
            0
            • R Raji

              @JonB : Thank you , but while writing to file param value saving into connection
              Ex: connection{
              type: " value"
              devip: " 113"
              param{
              }
              }
              getting like this, but i need devip inside param

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Raji
              But you don't show the code you are using to do this, do you? As I have already said, you want to find devip and add/remove/change inside the param node, and you are clearly not doing that and just putting it inside the connection node. Where does your code use param to locate that node?

              1 Reply Last reply
              1

              • Login

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