How to modify data and save to json file?
-
@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.