How to modify json data with out changing postion of keys , needs to change only values?
-
wrote on 9 Jul 2020, 08:37 last edited by
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
}
} -
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
}
}wrote on 9 Jul 2020, 08:45 last edited by JonB 7 Sept 2020, 08:50@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.
-
@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.
wrote on 9 Jul 2020, 09:24 last edited by@JonB :ok fine thanks,
how can i refer/change Param variables inside the Connections,in my above json filefor 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); -
@JonB :ok fine thanks,
how can i refer/change Param variables inside the Connections,in my above json filefor 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);wrote on 9 Jul 2020, 10:41 last edited by@Raji
As usual (for me), I don't understand the question! :( You change values in the JSON document either by using QJsonValueRef callsQJsonValueRef 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/4