[SOLVED] Empty QJsonDocument from file
-
Thanks for the quick answer.
Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.
-
If I load the data from the file to QDomDocument object it seems to work fine. I very puzzled.
-
Hi,
Just to be on the safe side, did you properly put your json file in the resources (qrc file) ?
You keep talking about QDomDocument, is it a typo ?
-
Yes, the file is properly put in my qrc file.
And yes, sorry, when I said
bq. Unfortunately, even though you are right, removing the superfluous comma did not solve the problem. The QDomDocument is still empty.
I meant the QJsonDocument is still empty.
But later on I tried to load the data into a QDomDocument object just to try and it seems to work fine.
@
QDomDocument *domDocument;
if (!domDocument->setContent(&file)) {
qDebug() << "Could not set DOM";
return;
}qDebug() << domDocument->toString();@
prints the file contents to the console.
Sorry for the confusion I created with the typo.
-
-
It returns
@""@ -
Then it seems that your file is empty
-
Might it be an encoding issue?
-
Strange, QJsonDocument should handle it...
Did you check what error you got when using fromJson ? -
No, I didn't. How could I do that?
-
Ok, I got it. A friend of mine pointed me out that I was looking for the array in the QJsonDocument, and that I should probably be looking for the object, which worked.
The only thing that doesn't work right is that
@QString jsonString = QString::fromUtf8(file.readAll());QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());@
works, but
@QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll());@
doesn't. -
Look at the second parameter of "fromJson":http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html#fromJson
From there you can get the error
-
It's an 'Illegal value' error. But I get the same error even with an empty file.
-
Could it be your json file encoding that is problematic ?
-
That might be it. Is there a way to make sure the file is UTF8? Or set it to UTF8?
-
If you are using QtCreator you could check the editor settings to ensure that it uses UTF8
-
Ah, that did it!
Thank you very much for all your help SGaist!
12/28