Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Qt reject a valid JSON?

Tags:

c++

json

qt

Using Qt-5.0, I have this JSON string

{"type":"FILE"}

I expected that fromBinaryData accept .toLocal8Bit() of the string as a valid format but it doesn't.

QString j = "{\"type\":\"FILE\"}";

auto doc = QJsonDocument::fromBinaryData(j.toLocal8Bit());

doc.isNull() // It's true, means the entry is not valid

Did I miss something?

like image 914
masoud Avatar asked Nov 29 '13 09:11

masoud


People also ask

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.


2 Answers

I have no idea of Qt, so I googled for a second. Here's what I found:

What you have is a string, a text representation. It's not the binary format Qt uses internally. The binary data would not be readable. QJsonDocument::fromBinaryData expects such a binary blob.

What you want to do seems to be achieved with QJsonDocument::fromJson which expects an UTF8-encoded Json string.

like image 67
Arne Mertz Avatar answered Sep 24 '22 15:09

Arne Mertz


Instead of fromBinaryData use fromJson with the same argument, I had this exact problem yesterday and that was what worked for me.

like image 44
Nicholas Smith Avatar answered Sep 22 '22 15:09

Nicholas Smith