Why does Qt reject valid JSON?

Using Qt-5.0, I have this JSON string

{"type":"FILE"} 

I expected fromBinaryData accept .toLocal8Bit() strings as a valid format, but it is not.

 QString j = "{\"type\":\"FILE\"}"; auto doc = QJsonDocument::fromBinaryData(j.toLocal8Bit()); doc.isNull() // It true, means the entry is not valid 

Did I miss something?

+6
source share
2 answers

I have no idea about Qt, so I searched for a second. Here is what I found :

You have a string, a textual representation. This is not the Qt binary format used internally. Binary data will not be readable. QJsonDocument::fromBinaryData expects such a binary blob.

What you want to do seems to be achieved with QJsonDocument::fromJson , which expects UTF8 encoded JSON encoding.

+9
source

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

+7
source

Source: https://habr.com/ru/post/959033/


All Articles