Can't convert from QJsonValue to int.
-
Hello I have a problem and I don't know how to solve it.
This is the JSON file:"toolBarColor": { "red": 156, "green": 244, "blue": 230 },
and this my Qt code:
QFile file(QCoreApplication::applicationDirPath() + "/settings.json"); QString input; file.open(QIODevice::ReadOnly | QIODevice::Text); input = file.readAll(); QJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())}; file.close(); QJsonObject jo{jsonDoc.object()}; QJsonValue tempVal{jo.value("toolBarColor")}; QJsonObject valueObj{tempVal.toObject()}; QJsonValue redVal{valueObj.value("red")}; QJsonValue greenVal{valueObj.value("green")}; QJsonValue blueVal{valueObj.value("blue")}; int red{redVal.toInt()}; int green{greenVal.toInt()}; int blue{blueVal.toInt()};
I don't know why, but this returns just the default value 0.
Can you please explain me how to solve this? -
@Christian-Ehrlicher I used that but the problem was that the file contained a JSON array first
-
You may always need to do the following in your code:
if ( false == file.open(QIODevice::ReadOnly | QIODevice::Text)) { print out open failed. return. } input = file.readAll(); if ( input.isEmpty() ) { print out empty file message return; } if ( true == jsonDoc.isEmpty() ) { print out message return }
-
@HenkCoder said in Can't convert from QJsonValue to int.:
QJsonValue tempVal{jo.value("toolBarColor")}; QJsonObject valueObj{tempVal.toObject()};
Check what these two lines set. Easy to do in debugger. And while you're there look in
QJsonValue redVal{valueObj.value("red")};
too!P.S.
Actually don't bother. You have not put any error checking in (you always should), you haven't checked the very top-levelQJsonDocument jsonDoc{QJsonDocument::fromJson(input.toUtf8())};
You should find that is returning an empty document (and setting an error for you)? Assuming that is your file verbatim you have a trailing
,
(comma) right at the end. That is not legal: this is JSON, not C++! :) -
@HenkCoder said in Can't convert from QJsonValue to int.:
This is the JSON file:
"toolBarColor": {
"red": 156,
"green": 244,
"blue": 230
},This is not a valid json file. Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.
-
@Christian-Ehrlicher You are right. It should be like this
{
"toolBarColor": {
"red": 156,
"green": 244,
"blue": 230
},
} -
@JoeCFD said in Can't convert from QJsonValue to int.:
JsonObject valueObj{jo["toolBarColor"]};
That is treating it as a list of objects or as an object with attribute
toolBarColor
, I can't recall which. I still think you are "lucky" it parses, as (so far as I know) you are not supposed to end any JSON list of things with a trailing comma....You have posted your last, and I still think you're not supposed to have the
}, }
... :)
-
@Christian-Ehrlicher Yes, I know, this is the full file:
{ "font": { "family": "Calibri", "pointSize": 11 }, "toolBarColor": { "red": 156, "green": 244, "blue": 230 }, "oldSplashscreen": false, "darkTheme": false }
-
@HenkCoder
Then if that is the full file naturally you must access viajo["toolBarColor"]
.Note that each time you use methods like
QJsonValue::toInt()
, and the otherQJsonValue::to...()
methods, you will get no error if the value is not of the type you think it is. Use theQJsonValue::is...()
methods to check,QJsonValue::type()
is your friend to tell you what you actually have. -
@JoeCFD said in Can't convert from QJsonValue to int.:
@JonB const QJsonValue QJsonDocument::operator[](const QString &key) const
https://doc.qt.io/qt-5/qjsondocument.html#operator-5b-5dI know this. Sorry, don't see the relevance.
-
@JonB to access that thing I did exactly what you said.
QJsonObject valueObj{jo["toolBarColor"].toObject()}; QJsonValue redVal{valueObj.value("red")}; QJsonValue greenVal{valueObj.value("green")}; QJsonValue blueVal{valueObj.value("blue")};
this is the code but i don't know if it is right.
-
Your code from the first post works fine for me. Make sure you're opening the correct json file. The conversion from/to QString is useless and wrong on non utf-8 systems though.
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QByteArray content = R"( { "font": { "family": "Calibri", "pointSize": 11 }, "toolBarColor": { "red": 156, "green": 244, "blue": 230 }, "oldSplashscreen": false, "darkTheme": false } )"; QJsonDocument jsonDoc{QJsonDocument::fromJson(content)}; QJsonObject jo{jsonDoc.object()}; QJsonValue tempVal{jo.value("toolBarColor")}; QJsonObject valueObj{tempVal.toObject()}; QJsonValue redVal{valueObj.value("red")}; QJsonValue greenVal{valueObj.value("green")}; QJsonValue blueVal{valueObj.value("blue")}; int red{redVal.toInt()}; int green{greenVal.toInt()}; int blue{blueVal.toInt()}; qDebug() << red << green << blue; // prints 156 244 230 return 0; }
-
@HenkCoder said in Can't convert from QJsonValue to int.:
@JonB Hello, I tried and there is no isInt() method to verify it, I don't really know now
So I said use QJsonValue::Type QJsonValue::type() const to check what type is in a
QJsonValue
before you assume you know what it is and use a non-erroring conversion method, especially when develeoping and it's not doing what you think....Here you are assuming its type is Double QMetaType::Double or QMetaType::LongLong But if it;s Null or Undefined you are not where you think you are...
-
Ooh now it works, I left a thing in the json file and forgot to save, I'm so dumb.
Thanks to all who tried to help me tho! -
@HenkCoder said in Can't convert from QJsonValue to int.:
I'm so dumb.
Please use the second parameter from QJsonDocument::fromJson() to get noticed about parser errors.
...