【发布时间】:2015-03-04 08:04:08
【问题描述】:
我正在尝试使用 Qt5 解析 this JSON Web-API,并使用 QJsonDocument 和 QJsonObject 解析 here。但我无法访问 QJsonObject 的 JSON 值。
这是我迄今为止尝试过的:
// Contains the whole API as QString...
QString data = QString(reply->readAll());
// Reads the JSON as QJsonDocument...
QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8());
// Reads the JSON as QJsonObject...
QJsonObject jsonObject = jsonResponse.object();
现在我已经准备好我的对象,但是尝试访问 JSON 的值却以某种方式失败了:
// This returns an empty string ""!?!
qDebug() << jsonObject.value("success").toString();
好吧,也许我把钥匙弄错了:
// Let's check the keys...
QStringList stringList = jsonObject.keys();
for (QStringList::Iterator it = stringList.begin(); it != stringList.end(); ++it)
{
// This returns "success" and "return" - huh!?!
qDebug() << *it;
}
好的,按键非常好,为什么不工作?
// Let's check the values by using the keys directly...
for (QStringList::Iterator it = stringList.begin(); it != stringList.end(); ++it)
{
// This returns empty strings "" and "" - now what?!?
qDebug() << jsonObject.value(*it).toString();
}
这又是没有意义的。我不明白为什么我不能通过键访问 JSON 对象的值。有什么想法吗?
我在其他 JSON API(例如 this one)上尝试了完全相同的代码,没有任何问题。我完全被困在这里了。
【问题讨论】:
标签: c++ json parsing qt5 key-value