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 Monday, May 27th 2025

How to Write Data to json file From Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 715 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.
  • R Offline
    R Offline
    Raji
    wrote on 24 Jul 2020, 13:25 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 25 Jul 2020, 19:07 last edited by
      #2

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

      (Z(:^

      R 1 Reply Last reply 27 Jul 2020, 02:17
      2
      • S sierdzio
        25 Jul 2020, 19:07

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

        R Offline
        R Offline
        Raji
        wrote on 27 Jul 2020, 02:17 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

        J 1 Reply Last reply 27 Jul 2020, 08:41
        0
        • R Raji
          27 Jul 2020, 02:17

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

          J Online
          J Online
          JonB
          wrote on 27 Jul 2020, 08:41 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 27 Jul 2020, 09:12
          1
          • J JonB
            27 Jul 2020, 08:41

            @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 27 Jul 2020, 09:12 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

            J 1 Reply Last reply 27 Jul 2020, 09:25
            0
            • R Raji
              27 Jul 2020, 09:12

              @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

              J Online
              J Online
              JonB
              wrote on 27 Jul 2020, 09:25 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

              1/6

              24 Jul 2020, 13:25

              • Login

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