Can't convert from QJsonValue to int.
-
My following code works
QJsonObject jo{jsonDoc.object()}; JsonObject valueObj{jo["toolBarColor"]}; QJsonValue redVal{valueObj.value("red")}; QJsonValue greenVal{valueObj.value("green")}; QJsonValue blueVal{valueObj.value("blue")};@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
}, }... :)
-
@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 Yes, I know, this is the full file:
{ "font": { "family": "Calibri", "pointSize": 11 }, "toolBarColor": { "red": 156, "green": 244, "blue": 230 }, "oldSplashscreen": false, "darkTheme": false } -
My following code works
QJsonObject jo{jsonDoc.object()}; JsonObject valueObj{jo["toolBarColor"]}; QJsonValue redVal{valueObj.value("red")}; QJsonValue greenVal{valueObj.value("green")}; QJsonValue blueVal{valueObj.value("blue")}; -
@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. -
@JonB const QJsonValue QJsonDocument::operator[](const QString &key) const
https://doc.qt.io/qt-5/qjsondocument.html#operator-5b-5d@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.
-
@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. -
@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.@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.
-
@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; } -
@JonB Hello, I tried and there is no isInt() method to verify it, I don't really know now
@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
QJsonValuebefore 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...
-
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; } -
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.
...
-
@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.
...
@Christian-Ehrlicher I used that but the problem was that the file contained a JSON array first
-
Your data has problem. Use mine( my test code works) to see if your code works.
{ "font": { "family": "Calibri", "pointSize": 11 }, "toolBarColor": { "red": 156, "green": 244, "blue": 230 }, "oldSplashscreen": false, "darkTheme": false }Are you working on Windows? the integer values may have some endings which can not be parsed properly by Qt code.
I manually changed them. My test code works.
In your output code add str = str.simplified() to get rid of them.