为什么Qt会拒绝有效的JSON?

mas*_*oud 8 c++ qt json

使用Qt-5.0,我有这个JSON字符串

{"type":"FILE"}
Run Code Online (Sandbox Code Playgroud)

我希望fromBinaryData接受.toLocal8Bit()字符串作为有效格式,但事实并非如此.

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

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

doc.isNull() // It's true, means the entry is not valid
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Arn*_*rtz 12

我不知道Qt,所以我用谷歌搜索了一秒钟.这是我发现的:

你拥有的是一个字符串,一个文本表示.它不是Qt内部使用的二进制格式.二进制数据不可读.QJsonDocument::fromBinaryData期待这样的二进制blob.

你想要做什么似乎是通过QJsonDocument::fromJsonUTF8编码的Json字符串来实现的.


Nic*_*ith 8

我没有fromBinaryData使用fromJson相同的论点,而是昨天遇到了这个问题,这对我有用.