Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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
    }
    }



  • @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.



  • @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);



  • @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.


Log in to reply