How to Write Data to json file From Qt
-
wrote on 24 Jul 2020, 13:25 last edited by
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 .
-
Are you getting any errors? Or resulting file is wrong? Please provide more information.
-
@sierdzio Result file is wrong , i want to modify/insert the below data values of both at same time
connection->type
connection->param->devipwrote on 27 Jul 2020, 08:41 last edited by@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 insideparam
in just the same way thatconnection->param
is a keyed object insideconnection
. You can eitherremove()
and re-insert()
yourtype
&devip
inside their parents, or you can useQJsonValueRef
to overwrite the value already there. Then you save back when you have done both. -
@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 insideparam
in just the same way thatconnection->param
is a keyed object insideconnection
. You can eitherremove()
and re-insert()
yourtype
&devip
inside their parents, or you can useQJsonValueRef
to overwrite the value already there. Then you save back when you have done both. -
@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 paramwrote on 27 Jul 2020, 09:25 last edited by@Raji
But you don't show the code you are using to do this, do you? As I have already said, you want to finddevip
and add/remove/change inside theparam
node, and you are clearly not doing that and just putting it inside theconnection
node. Where does your code useparam
to locate that node?
1/6