PDA

View Full Version : reading JSON file in Qt5



LZAntal
18th January 2013, 06:36
Hi,

I have been trying to figure out how to read a JSON file using Qt5 QJsonDocument with no success so far.
I can't find any examples online. If someone could give me a simple example I would appreciate it.

Here is my code::

QFile file;
file.setFileName("/tmp/settings.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QJsonDocument settdoc;
settdoc.fromBinaryData(file.readAll());
qDebug() << settdoc.isNull();

settdoc.isNull() returns true, I can read the file into a stream without any issue and I validated the json as well.

Thank you

Laszlo

Lykurg
18th January 2013, 07:45
Does /tmp/settings.json exists? Is it well formed? Also try QJsonDocument::fromJson and see what error (QJsonParseError) you get.

EDIT: Forget the first questions....

EDIT 2:
QString str = "{"
" \"Herausgeber\": \"Xema\","
" \"Nummer\": \"1234-5678-9012-3456\","
" \"Deckung\": 2e+6,"
" \"Währung\": \"EURO\","
" \"Inhaber\": {"
" \"Name\": \"Mustermann\","
" \"Vorname\": \"Max\","
" \"männlich\": true,"
" \"Hobbys\": [ \"Reiten\", \"Golfen\", \"Lesen\" ],"
" \"Alter\": 42,"
" \"Kinder\": [],"
" \"Partner\": null"
" }"
"}";
QJsonDocument d = QJsonDocument::fromJson(str.toUtf8());
qWarning() << d.isNull(); See if that works for you and then replace str.toUtf8() through your readAll().

LZAntal
18th January 2013, 15:17
Thank you for your fast reply and even more thank you for your example code!!
It works now! I somehow overlooked that I can use toUtf8() on a QString.
You example put me in the right track and now I can adjust basic window properties with my json file :D
Now just need to work on auto copy this file into the apps resources at runtime.

Here is my working code::



QString settings;
QFile file;
file.setFileName("/tmp/settings.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
settings = file.readAll();
file.close();

QJsonDocument sd = QJsonDocument::fromJson(settings.toUtf8());
qWarning() << sd.isNull(); // <- print false :)
QJsonObject sett2 = sd.object();
qWarning() << sett2.value(QString("title")); // <- print my title



Thanks again, totally made my morning :)

Laszlo

anda_skoa
21st January 2013, 19:20
You are aware that this requires two codec conversions, right?



settings = file.readAll()


converts from UTF-8 to UTF-16 and



settings.toUtf8()


converts from UTF-16 to UTF-8.

Since start and end encoding are the same (UTF-8) those two conversions are unneeded.

Why don't you just call fromJson() with the QByteArray returned by file.readAll()?

Also: the problem with your original code has nothing to do with the content at all, you are calling a static method and are then wondering why it didn't change an instance.

Static methods can't do that.
Hint: in Qt methods that are called fromABCD usually return something.

Cheers,
_

velandrea
21st August 2017, 20:44
QString filename = QFileDialog::getOpenFileName();

QString val;
QFile file;
file.setFileName(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject Documento = d.object();

//{}= objet [] Array