Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to modify json data with out changing postion of keys , needs to change only values?
Forum Updated to NodeBB v4.3 + New Features

How to modify json data with out changing postion of keys , needs to change only values?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.6k 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 last edited by
    #1

    i want to modify only values for this json data?please any one can 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
    }
    },
    "system":{
    "mode":"reprocessing",
    "dbc":{
    "file":"LGSim_CAN.dbc",
    "table":"LGSim_CAN.table"
    }
    },
    "reprocess":{
    "type":"ROS-IF",
    "param":{
    "ip":"127.0.0.1",
    "port":9090,
    "width":1280,
    "height":720,
    "pixelformat":10
    }
    }

    JonBJ 1 Reply Last reply
    0
    • R Raji

      i want to modify only values for this json data?please any one can 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
      }
      },
      "system":{
      "mode":"reprocessing",
      "dbc":{
      "file":"LGSim_CAN.dbc",
      "table":"LGSim_CAN.table"
      }
      },
      "reprocess":{
      "type":"ROS-IF",
      "param":{
      "ip":"127.0.0.1",
      "port":9090,
      "width":1280,
      "height":720,
      "pixelformat":10
      }
      }

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Raji
      JSON has no ordering of keys. The order keys come out when serializing to text is "random"/"undefined", and there is nothing you can do to affect that. If they come out in the same order each time, and that suits you, you are "lucky". If anything relies on the order, it's wrong.

      To modify values from a JSON text file, you must read it in, parse it into JSON object tree, make your changes in-memory, and export the whole thing back to file. There is no other way.

      R 1 Reply Last reply
      2
      • JonBJ JonB

        @Raji
        JSON has no ordering of keys. The order keys come out when serializing to text is "random"/"undefined", and there is nothing you can do to affect that. If they come out in the same order each time, and that suits you, you are "lucky". If anything relies on the order, it's wrong.

        To modify values from a JSON text file, you must read it in, parse it into JSON object tree, make your changes in-memory, and export the whole thing back to file. There is no other way.

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

        @JonB :ok fine thanks,
        how can i refer/change Param variables inside the Connections,in my above json file

        for connections i am using this format
        QJsonObject RootObject = JsonDocument.object();
        QJsonValueRef ref = RootObject.find("connection").value();
        QJsonObject m_addvalue = ref.toObject();
        m_addvalue.insert("type",mType);/ /set the value you want to modify
        ref=m_addvalue; //assign the modified object to reference
        JsonDocument.setObject(RootObject);

        JonBJ 1 Reply Last reply
        0
        • R Raji

          @JonB :ok fine thanks,
          how can i refer/change Param variables inside the Connections,in my above json file

          for connections i am using this format
          QJsonObject RootObject = JsonDocument.object();
          QJsonValueRef ref = RootObject.find("connection").value();
          QJsonObject m_addvalue = ref.toObject();
          m_addvalue.insert("type",mType);/ /set the value you want to modify
          ref=m_addvalue; //assign the modified object to reference
          JsonDocument.setObject(RootObject);

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

          @Raji
          As usual (for me), I don't understand the question! :( You change values in the JSON document either by using QJsonValueRef calls

          QJsonValueRef is a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference.

          or by updating your "back-end" object representation and regenerating the whole JSON document as you did to produce the saved JSON file in the first place.

          1 Reply Last reply
          2

          • Login

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