Qt 中使用 QVariantMap 的 JSON
我想在 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;
}
I would like to parse this JSON output in a symbian application:
[
{"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"}
]
For this, I wrote following code, but I can't read the data. Other single JSON output it works fine, but a multiple output doesn't work:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在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
有人else 还指出可以使用 QtScript 解析 JSON 数据。如果你看得懂中文(如果看不懂,请谷歌翻译):http://www .cuteqt.com/blog/?p=2261(该网站暂时关闭,忘记通过Google Reader备份,GR也死了...2013-07-02)
In Qt 5, it has the support for JSON data.
"Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet.
The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data. It also contains support for saving this data in a binary format that is directly mmap'able and very fast to access.
More details about the JSON data format can be found at and in RFC-4627."
http://qt-project.org/doc/qt-5/json.html
And I think it's not difficult to port them to Qt 4 if you need.
At least you can find the code at https://github.com/qtproject/qtbase/tree/5.3/src/corelib/json
Someone ported QJson* from 5 to 4:(Added 2013-07-02)
https://github.com/5in4/qjson-backport
Note: 2012-11-30
Someone else also pointed that it's possible to use QtScript to parse JSON data. If you can read Chinese(if can't, please Google Translate it): http://www.cuteqt.com/blog/?p=2261 (This site is down for now, forgot to backup it via Google Reader, and GR is also dead... 2013-07-02)
您正在使用 https://github.com/ereilin/qt-json 中的解析器,正确的?据我所知,该解析器需要您的 JSON 数据始终是顶层的对象,因此您的数据需要类似于查看
Qt 的最佳 JSON 解析器? 对于一些替代解析器,我建议看看 qjson (http://qjson.sourceforge.net/)。
You are using the parser from https://github.com/ereilin/qt-json, right? As far as I am aware, that parser needs your JSON data to always be an object at the top-level, so your data needs to look something like
Check out the answers to Best JSON parser for Qt? for some alternative parsers, I'd recommend taking a look at qjson (http://qjson.sourceforge.net/).
用于传递:
used for passing:
您正在尝试像这样迭代 QVariantMap:
QMapIterator; i(结果);
...但是 QVariantMap 是基于变体的,所以你需要:
QMapIterator;我(结果);
然后,在循环中,您将访问 int 中的值,如下所示:
<代码>cout << i.key() << “:”<< i.value().toInt() <<结束;
You are trying to iterate over the QVariantMap like so:
QMapIterator<QString, int> i(result);
...but a QVariantMap is variant based, so you would need:
QMapIterator<QString, QVariant> i(result);
Then, in the loop, you would access the value as in int like so:
cout << i.key() << ": " << i.value().toInt() << endl;