masoud
masoud

Reputation: 56539

Why does Qt reject a valid JSON?

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?

Upvotes: 8

Views: 2448

Answers (2)

Nicholas Smith
Nicholas Smith

Reputation: 11764

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

Upvotes: 8

Arne Mertz
Arne Mertz

Reputation: 24626

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.

Upvotes: 12

Related Questions