这是我的json的结构:
{
"images": [
{
"imagename": "banner.jpg",
"shapes": [
]
},
{
"imagename": "banner.png",
"shapes": [
]
},
{
"imagename": "logo.png",
"shapes": [
]
}
]
}这是我的代码:
QString filename = jsonFilePath;
QString val;
QFile file;
file.setFileName(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
qDebug() << d;当我运行上面的代码,并尝试使用qDebug()输出d时,打印到控制台的所有内容都是QJsonDocument()。
发布于 2020-04-09 04:30:48
如果没有运行时错误,您的代码可以正常工作。我建议检查运行时错误:
JSON检查file.open().
QJsonDocument::fromJson()的第二个参数以捕获解析错误并像这样打印它们: QJsonParseError err;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8(), &err);
qDebug() << d << err.errorString();希望这有助于捕获错误。
发布于 2020-04-17 03:53:39
我建议在打印QJsonDocument时使用toJson()方法
qDebug() << "Json Document:" << d.toJson();https://stackoverflow.com/questions/61106932
复制相似问题