[SOLVED] Empty QJsonDocument from file
-
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.
-
Then it seems that your file is empty
-
Strange, QJsonDocument should handle it...
Did you check what error you got when using fromJson ? -
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
-
Could it be your json file encoding that is problematic ?
-
If you are using QtCreator you could check the editor settings to ensure that it uses UTF8