我想在symbian应用程序中解析这个JSON输出:
[
{"ID":"9","titel":"wouter","plaatsID":"2616","prio":"3"},
{"ID":"8","titel":"pasta","plaatsID":"3780","prio":"3"},
{"ID":"6","titel":"Muts prikken","plaatsID":"3780","prio":"2"
{"ID":"5","titel":"doorplannen","plaatsID":"3840","prio":"2"}
{"ID":"4","titel":"Gasfles","plaatsID":"3780","prio":"2"}
]
为此,我编写了以下代码,但我无法读取数据。其他单个JSON输出它工作正常,但多输出不起作用:
void start::finishedSlot(QNetworkReply * reply)
{
// Reading attributes of the reply
// e.g. the HTTP status code
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (reply->error() == QNetworkReply::NoError)
{
QByteArray data = reply->readAll();
bool ok;
QVariantMap result = Json::parse(QString(data), ok).toMap();
if(!ok) {
qFatal("An error occurred during parsing");
exit(1);
}
QMapIterator<QString, int> i(result);
while (i.hasNext()) {
i.next();
cout << i.key() << ": " << i.value() << endl;
}
ui->log->setText("het gaat goed");
}
// Some http error received
else
{
ui->log->setText("gaat NIET goed");
}
delete reply;
}
答案 0 :(得分:6)
在Qt 5中,它支持JSON数据。
“Qt为处理JSON数据提供支持.JSON是一种编码从Javascript派生的对象数据的格式,但现在广泛用作互联网上的数据交换格式。 Qt中的JSON支持提供了一个易于使用的C ++ API来解析,修改和保存JSON数据。它还支持以二进制格式保存此数据,该格式可直接映射,访问速度非常快。 有关JSON数据格式的更多细节可以在RFC-4627中找到。“
http://qt-project.org/doc/qt-5/json.html
如果你需要,我认为将它们移植到Qt 4并不困难。
至少你可以在https://github.com/qtproject/qtbase/tree/5.3/src/corelib/json
找到代码有人将QJson *从5移到4 :(已添加2013-07-02) https://github.com/5in4/qjson-backport
注:2012-11-30
其他人也指出可以使用QtScript来解析JSON数据。如果您可以阅读中文(如果不能,请谷歌翻译):http://www.cuteqt.com/blog/?p=2261(此网站暂时停用,忘记通过谷歌阅读器备份,GR也死了...... 2013-07- 02)
答案 1 :(得分:3)
您正在使用https://github.com/ereilin/qt-json中的解析器,对吗?据我所知,该解析器需要您的JSON数据始终是顶级对象,因此您的数据需要看起来像
{"somename": [
{"ID":"9","titel":"wouter","plaatsID":"2616","prio":"3"},
{"ID":"8","titel":"pasta","plaatsID":"3780","prio":"3"},
{"ID":"6","titel":"Muts prikken","plaatsID":"3780","prio":"2"},
{"ID":"5","titel":"doorplannen","plaatsID":"3840","prio":"2"},
{"ID":"4","titel":"Gasfles","plaatsID":"3780","prio":"2"}
] }
查看Best JSON parser for Qt?对某些替代解析器的答案,我建议您查看qjson(http://qjson.sourceforge.net/)。
答案 2 :(得分:1)
void parse_links(const QScriptValue & value, QList<Link> & cbk_links)
{
QList<QVariantMap> list;
qScriptValueToSequence(value,list);
foreach(auto item, list)
{
Link link;
link.yawDeg = item.value("yawDeg").toFloat();
link.panoId = item.value("panoId").toString();
link.road_argb = item.value("road_argb").toString();
link.description = item.value("description").toString();
link.scene = item.value("scene").toInt();
cbk_links.append(link);
}
}
用于传球:
"Links": [
{ "yawDeg":"18.49",
"panoId":"Voal3KQo5FNL67hq7tA8nA",
"road_argb":"0x80ffffff",
"description":"Knuth-Wintherfeldts Allé",
"scene":"0"
}, { "yawDeg":"198.49",
"panoId":"6RCsAsNoawmh98eOOs7Wzw",
"road_argb":"0x80ffffff",
"description":"Knuth-Wintherfeldts Allé",
"scene":"0"
}
]