[SOLVED]QJsonDocument, QJsonValue, QJsonObject: How To Change Value from File
-
I just put 10 in the example. I've tried bool, string, double... nothing works. Im using 5.2.1. And did I mention that nothing seems to work and my compiler complains regardless of how i try to assign. there appears to be no way to update the on an already created QJsonValue object... Ive never seen an object that wont let you assign to it as a way of preventing you from assigning the wrong thing. That seems like sewing someones mouth shut to prevent them from eating poison. Just sayin.
-
But thank you for your response JKSH
-
Sorry, you asked my compiler: Microsoft Visual C++ 11(amd64) and my kit is Desktop Qt 5.2.1 MSVC2012 OpenGL 64 bit.
Here's the full source, Ive tried all your recommendations and nothing works:
@
void MyConfig::configValueChangedHandler(QString name, bool updateVal){
QFile configFile("MyConfig.json");
QString inputData;if(configFile.open(QIODevice::ReadWrite | QIODevice::Text)) { inputData = configFile.readAll(); QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8()); QJsonObject jo = jsonDoc.object(); QJsonValue value = jo.value(QString(name)); //value = value.toBool(updateVal);
// jo.value(QString(name)) = jo.value(name).toBool(updateVal);
// value = jo.value(QString(name));
// value = value.operator =(updateVal);
//value = updateVal;// QJsonValue subobj = item[name];
// subobj. = updateVal;//item[name] = updateVal; configFile.write(jsonDoc.toJson()); configFile.close(); }
}
@I've left all the attempts to assign a value back to value in place. Thanks for your help. Still doesnt seem to be a way to assign a value to QJsonValue, or QJsonObject
EDIT: Enclosed code in '@' tags -- JKSH
-
Ah, I JUST uninstalled MSVC 2012 from my system yesterday!
Anyway, I made minor modifications to your code to make a fully-standalone program...
@
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>int main(int argc, char *argv[])
{
QString name;
bool updateVal;QFile configFile("MyConfig.json"); QString inputData; if(configFile.open(QIODevice::ReadWrite | QIODevice::Text)) { inputData = configFile.readAll(); QJsonDocument jsonDoc = QJsonDocument::fromJson(inputData.toUtf8()); QJsonObject jo = jsonDoc.object(); QJsonValue value = jo.value(QString(name)); value = value.toBool(updateVal); jo.value(QString(name)) = jo.value(name).toBool(updateVal); value = jo.value(QString(name)); value = value.operator =(updateVal); value = updateVal;
// QJsonValue subobj = item[name];
// subobj = updateVal;// item[name] = updateVal;
configFile.write(jsonDoc.toJson());
configFile.close();
}return 0;
}
@...and successfully compiled it with MSVC 2013 64-bit + Qt 5.3.0 beta, as well as MinGW 4.8 32-bit + Qt 5.2.1. The only issues I encountered were warnings about unused variables/parameters.
There's nothing invalid with your code, as far as I can see.
Perhaps some binary files simply got corrupted -- Try starting a new project and creating a minimal example.
-
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!"})
@