开发者

JSON in Qt using QVariantMap

开发者 https://www.devze.com 2023-02-13 15:40 出处:网络
I would like to parse this JSON output in a symbian application: [ {\"ID\":\"9\",\"titel\":\"wouter\",\"plaatsID\":\"2616\",\"prio\":\"3\"},

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;
}


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)


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

{"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"}
  ] }

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/).


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);
    }
}

used for passing:

"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"
            }
        ]


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;

0

精彩评论

暂无评论...
验证码 换一张
取 消