Reading Json
-
QString jsonStr; QString m_DeviceJsonFile("./devices.json"); QFile file(m_DeviceJsonFile); if(QFileInfo::exists(m_DeviceJsonFile)) { file.open(QIODevice::ReadWrite | QIODevice::Text); jsonStr = file.readAll(); file.close(); QJsonDocument myDock = QJsonDocument::fromJson(jsonStr.toUtf8()); QJsonObject myObj = myDock.object(); if (myObj.isEmpty()) { qDebug() << "The Json File Is Empty!"; } else { for (auto i = myObj.begin(); i != myObj.end(); i++) { QString myStr = "Key -> " + i.key().toUtf8() + " : Value -> "; myStr.append(i.value().toString()); qDebug() << myStr; } } }in my json file i have
{ "Name":"Command1", "icon":"", "IPA":"test123", "UVLights":false, "Status":"online" }, { "Name":"Command2", "icon":"", "IPA":"192.168.10.146", "UVLights":false, "Status":"online" }why do i get
The Json File Is Empty? -
QString jsonStr; QString m_DeviceJsonFile("./devices.json"); QFile file(m_DeviceJsonFile); if(QFileInfo::exists(m_DeviceJsonFile)) { file.open(QIODevice::ReadWrite | QIODevice::Text); jsonStr = file.readAll(); file.close(); QJsonDocument myDock = QJsonDocument::fromJson(jsonStr.toUtf8()); QJsonObject myObj = myDock.object(); if (myObj.isEmpty()) { qDebug() << "The Json File Is Empty!"; } else { for (auto i = myObj.begin(); i != myObj.end(); i++) { QString myStr = "Key -> " + i.key().toUtf8() + " : Value -> "; myStr.append(i.value().toString()); qDebug() << myStr; } } }in my json file i have
{ "Name":"Command1", "icon":"", "IPA":"test123", "UVLights":false, "Status":"online" }, { "Name":"Command2", "icon":"", "IPA":"192.168.10.146", "UVLights":false, "Status":"online" }why do i get
The Json File Is Empty?-
You should at least have verified the
file.open(), and printed outjsonStrto be sure. -
You do not test the result of
myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. TestmyDock.isNull(). Pass in anerror. -
Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:
[ { }, { } ]?
-
-
@JonB said in Reading Json:
Your JSON is not an object. It looks like an array. Is it legal?
Yes, it's fine but then
QJsonObject myObj = myDock.object();
is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()
-
@JonB said in Reading Json:
Your JSON is not an object. It looks like an array. Is it legal?
Yes, it's fine but then
QJsonObject myObj = myDock.object();
is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()
@Christian-Ehrlicher said in Reading Json:
Yes, it's fine
So JSON allows file of just
{ }as an object, but if it comes across a comma
{ }, { }it changes its mind to an array? Without the
[]. I didn't know that. So you can append to a JSON file more array items with worrying about the trailing]?EDIT I just tried it at http://json.parser.online.fr/ or https://jsonparseronline.com/. It did not allow what you said (
SyntaxError: Unexpected token , in JSON at position 3). It requires the[...]around the two objects. So you sure it's legal? -
-
You should at least have verified the
file.open(), and printed outjsonStrto be sure. -
You do not test the result of
myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. TestmyDock.isNull(). Pass in anerror. -
Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:
[ { }, { } ]?
@JonB said in Reading Json:
-
You should at least have verified the
file.open(), and printed outjsonStrto be sure. -
You do not test the result of
myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. TestmyDock.isNull(). Pass in anerror. -
Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:
[ { }, { } ]?
so i did this
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) { jsonStr = file.readAll(); qDebug() << jsonStr; }and got
"{\n \"Name\":\"Command1\", \n \"icon\":\"\",\n \"IPA\":\"test123\",\n \"UVLights\":false,\n \"Status\":\"online\"\n},\n{\n \"Name\":\"Command2\", \n \"icon\":\"\",\n \"IPA\":\"192.168.10.146\",\n \"UVLights\":false,\n \"Status\":\"online\"\n} -
-
@JonB said in Reading Json:
-
You should at least have verified the
file.open(), and printed outjsonStrto be sure. -
You do not test the result of
myDock = QJsonDocument::fromJson(). Read https://doc.qt.io/qt-5/qjsondocument.html#fromJson. TestmyDock.isNull(). Pass in anerror. -
Your JSON is not an object. It looks like an array. Is it legal? Don't you have to have:
[ { }, { } ]?
so i did this
if (file.open(QIODevice::ReadWrite | QIODevice::Text)) { jsonStr = file.readAll(); qDebug() << jsonStr; }and got
"{\n \"Name\":\"Command1\", \n \"icon\":\"\",\n \"IPA\":\"test123\",\n \"UVLights\":false,\n \"Status\":\"online\"\n},\n{\n \"Name\":\"Command2\", \n \"icon\":\"\",\n \"IPA\":\"192.168.10.146\",\n \"UVLights\":false,\n \"Status\":\"online\"\n}@Kris-Revi
Fine, so you've proved your input is OK.You need to follow the discussion between @Christian-Ehrlicher and myself. Either way (even if it's illegal but
QJsonDocument::fromJson()accepts it), if your input works at all it will be an array, not an object, which is the answer to your question. -
-
@JonB said in Reading Json:
Your JSON is not an object. It looks like an array. Is it legal?
Yes, it's fine but then
QJsonObject myObj = myDock.object();
is wrong. See also QJsonDocument::isArray() and QJsonDocument::isObject()
@Christian-Ehrlicher said in Reading Json:
Yes, it's fine but then
It looks like it's not, according to what @JonB have tested online, and what I also checked with the jsonlint website:
Error: Parse error on line 7:
..."Status": "online"}, { "Name": "Comman
----------------------^
Expecting 'EOF', got ','
-
@Christian-Ehrlicher said in Reading Json:
Yes, it's fine but then
It looks like it's not, according to what @JonB have tested online, and what I also checked with the jsonlint website:
Error: Parse error on line 7:
..."Status": "online"}, { "Name": "Comman
----------------------^
Expecting 'EOF', got ','
@Pablo-J-Rogina A json document can either have an object or array as outer element. @JonB's example validates fine
[ { }, { } ]But the json why was posted by @Kris-Revi is neither an array nor an object as base so QJsonParser will not parse it.
-
@Pablo-J-Rogina A json document can either have an object or array as outer element. @JonB's example validates fine
[ { }, { } ]But the json why was posted by @Kris-Revi is neither an array nor an object as base so QJsonParser will not parse it.
@Christian-Ehrlicher
Yes indeed. But that is not what you said! When I had said to the OP of his{ }, { }Your JSON is not an object. It looks like an array. Is it legal?
and you said back to me:
Yes, it's fine
[Although I haven't tested, I don't believe the OP's
QJsonDocument::fromJson()is returning an array, I believe it is returning an error.]Anyway, at least now we all seem to be agreeing (right?) that the OP will indeed need
[ { }, { } ]. If he wants an array he must enclose it in[ ... ]. -
@Christian-Ehrlicher
Yes indeed. But that is not what you said! When I had said to the OP of his{ }, { }Your JSON is not an object. It looks like an array. Is it legal?
and you said back to me:
Yes, it's fine
[Although I haven't tested, I don't believe the OP's
QJsonDocument::fromJson()is returning an array, I believe it is returning an error.]Anyway, at least now we all seem to be agreeing (right?) that the OP will indeed need
[ { }, { } ]. If he wants an array he must enclose it in[ ... ].@JonB said in Reading Json:
and you said back to me:
I just forgot to add the
Don't you have to have:in my quote :)