[SOLVED]QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File
-
LOL... I swear I tried that too... But Ill give it another shot
-
your code seems like you are doing the same thing over and over... granted Im fairly rusty with my C++ but it seems like 5 lines of code to do where a single @value = updateVal@ should suffice. That doesnt seem efficient to me. Ill let you know if it works
-
[quote author="doozii" date="1395837642"]your code seems like you are doing the same thing over and over... granted Im fairly rusty with my C++ but it seems like 5 lines of code to do where a single @value = updateVal@ should suffice. That doesnt seem efficient to me. Ill let you know if it works[/quote]That's your code -- I simply moved it into main() and un-commented lines.
My main point was this: There is no compilation error. All of the different ways to assign something to QJsonValue are valid.
(The actual code I would use is in my first reply, just below your original post)
-
ohhh I see. yeah I was trying to just use @value = updateVal;@ I left all the commented out lines in to show what i had tried.
-
So I believe you are correct. I missed your comment about not being able to initialize with pointers and I was passing a pointer. Did not realize it wouldnt work with a reference. I tried again and all of the things I tried seem to work now.
Thank you so much for working through this with me.
-
I'm glad you've found a solution :)
All the best with your project!
-
I got this to compile but there is no value getting written to the QJSonValue and/or the value isnt getting written back to the QJsonDocument. So when I do the @value = val@ and I look at the value variable... its empty.
I wish there was example code for this somewhere or someone would write a book and include JSON handling in it.
-
It's not quite a book, but see the official unit tests for a comprehensive suite of JSON assignments/manipulations: http://code.woboq.org/qt5/qtbase/tests/auto/corelib/json/tst_qtjson.cpp.html
How did you look at the QJsonValue?
-
i did a qWarning() << value.toString(); and a qWarning() << value; Also tried qWarning() << jo[name] and a host of others. All returned as ""
-
Code:
@
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>int main()
{
QFile file("test.json");
if (file.open(QFile::ReadOnly|QFile::Text))
{
QByteArray jsonText = file.readAll();
file.close();QJsonDocument doc = QJsonDocument::fromJson(jsonText); qDebug() << doc; QJsonObject obj = doc.object(); qDebug() << obj; QJsonValue val = obj["dblValue"]; qDebug() << val; qDebug() << "-----"; val = 1.61803; qDebug() << val; obj["dblValue"] = val; qDebug() << obj; doc.setObject(obj); qDebug() << doc; if (file.open(QFile::WriteOnly|QFile::Text)) { file.write(doc.toJson()); file.close(); } } return 0;
}
@Input file:
@
{
"boolValue": true,
"dblValue": 3.14159,
"strValue": "Hello!"
}
@Output:
@
QJsonDocument({"boolValue":true,"dblValue":3.1415899999999999,"strValue":"Hello!"})
QJsonObject({"boolValue":true,"dblValue":3.1415899999999999,"strValue":"Hello!"})
QJsonValue(double, 3.14159)QJsonValue(double, 1.61803)
QJsonObject({"boolValue":true,"dblValue":1.6180300000000001,"strValue":"Hello!"})
QJsonDocument({"boolValue":true,"dblValue":1.6180300000000001,"strValue":"Hello!"})
@