[SOLVED] Empty QJsonDocument from file
-
Hi,
I'm trying to parse an appSettigns.json file that holds some application settings, but when I set the json document with QJsonDocument::fromJson(file.readAll()) the json document stays empty.
Here's the code:
jsonmanager.cpp
@
#include "jsonmanager.h"#include <QFile>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>JsonManager::JsonManager(QWidget *parent)
{
QFile file(":/config/appSettings.json");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Could not open file";
qDebug() << file.errorString();
return;
}QJsonDocument jsonDoc; jsonDoc = QJsonDocument::fromJson(file.readAll()); if (!jsonDoc.isNull()) { qDebug() << "Could not read data"; } QJsonObject jsonObj = jsonDoc.object(); QVariantMap jsonMap = jsonObj.toVariantMap(); qDebug() << jsonMap.size(); // outputs 0 // and so does jsonDoc.array().size(); file.close();
}
@appSettings.json
@{
"osc_port":57120,
"server": {
"path": "/path/to/executable",
"ip": "127.0.0.1",
"port": 54321
},
"data_path": "/path/to/data/folder",
"frame_rate": 25,
"vertical_sync": true,
"threshold": 64,
"camera": {
"device_id": 0,
"width": 640,
"height": 480,
"rotate_180": false,
"use_uvc": false
},
"background_color": {
"red": 64,
"green": 64,
"blue": 64,
"alpha": 255,
},
"tool_highlight_color": {
"red": 240,
"green": 64,
"blue": 64,
"alpha": 128,
}
}
@I also checked the array in the QJsonDocument but it's empty, too. Any hints on what I'm doing wrong?
Thanks!
-
In the background_color object, there is a superfluous comma, as well as in tool_highlight_color. Maybe that is your problem.
-
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 ?