How to modify data and save to json file?
-
I loaded json file, shown it to listmodel and I want to save data in json file.
Help me! -
Hi,
What exactly did you implement ?
-
I loaded json file, shown it to listmodel and I want to save data in json file.
Help me!Hi @Alex-Dragon ,
You suppose to use QJsonValueReference to modify the json data.{ "FirstName": "John", "LastName": "Doe", "Age": 43, "Address": { "Street": "Downing Street 10", "City": "London", "Country": "Great Britain" }, "Phone numbers": [ "+44 1234567", "+44 2345678" ] }
suppose now if you need to change the street name, use the following code.
QFile file(filepath); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError JsonParseError; QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError); file.close(); QJsonObject RootObject = JsonDocument.object(); QJsonValueRef ref = RootObject.find("Address").value(); QJsonObject m_addvalue = ref.toObject(); m_addvalue.insert("Street","India");//set the value you want to modify ref=m_addvalue; //assign the modified object to reference JsonDocument.setObject(RootObject); // set to json document file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate); file.write(JsonDocument.toJson()); file.close();
try this and let me know your result.
Happy coding. -
Hi @Alex-Dragon ,
You suppose to use QJsonValueReference to modify the json data.{ "FirstName": "John", "LastName": "Doe", "Age": 43, "Address": { "Street": "Downing Street 10", "City": "London", "Country": "Great Britain" }, "Phone numbers": [ "+44 1234567", "+44 2345678" ] }
suppose now if you need to change the street name, use the following code.
QFile file(filepath); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError JsonParseError; QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll(), &JsonParseError); file.close(); QJsonObject RootObject = JsonDocument.object(); QJsonValueRef ref = RootObject.find("Address").value(); QJsonObject m_addvalue = ref.toObject(); m_addvalue.insert("Street","India");//set the value you want to modify ref=m_addvalue; //assign the modified object to reference JsonDocument.setObject(RootObject); // set to json document file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate); file.write(JsonDocument.toJson()); file.close();
try this and let me know your result.
Happy coding. -
@sharath Really awesome code snippet! Saved me lots of time, was exactly what I was looking for, and it worked great. Thanks!!!!
@josht000 please don't forget to mark your post as solved!
-
@josht000 please don't forget to mark your post as solved!
@Pablo-J-Rogina it's not the original author of this thread ;-)
-
@yashnikam In the same way it is done for address...
-
@SGaist I want to change the value of FirstName and Age , it has no object to find , pls help me !
@the-woft
FirstName
andAge
are in theRootObject
just likeAddress
is. Their values will be a string and an integer respectively, so retrievable via QString QJsonValue::toString() const & int QJsonValue::toInt(int defaultValue = 0) const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key). -
@the-woft
FirstName
andAge
are in theRootObject
just likeAddress
is. Their values will be a string and an integer respectively, so retrievable via QString QJsonValue::toString() const & int QJsonValue::toInt(int defaultValue = 0) const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).@JonB said in How to modify data and save to json file?:
FirstName and Age are in the RootObject just like Address is. Their values will be strings, so retrievable via QString QJsonValue::toString() const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).
i'm newbie in qt and i try it but not correct , you can give me sample code do it ?
-
@JonB said in How to modify data and save to json file?:
FirstName and Age are in the RootObject just like Address is. Their values will be strings, so retrievable via QString QJsonValue::toString() const and modifiable via QJsonValueRef QJsonObject::operator[](const QString &key).
i'm newbie in qt and i try it but not correct , you can give me sample code do it ?
@the-woft said in How to modify data and save to json file?:
and i try it but not correct
Then show what you tried.
There is example code in documentation and example applications like https://doc.qt.io/qt-6/qtcore-serialization-savegame-example.html -
@the-woft said in How to modify data and save to json file?:
and i try it but not correct
Then show what you tried.
There is example code in documentation and example applications like https://doc.qt.io/qt-6/qtcore-serialization-savegame-example.html -
@jsulm this my code
QJsonValue Age = root_object.value("Age");
QVariant tmp = Age.toVariant();
qlonglong result = root_object.value("Age").toVariant().toLongLong();
QJsonValueRef ref = RootObject("result");
QJsonObject m_addvalue = ref.toObject(); -
@the-woft
You read theAge
entry inroot_object
but where do you set theAge
entry inroot_object
? You don't. I gave you the reference to QJsonValueRef QJsonObject::operator[](const QString &key) to use.