The problem of parsing json with libqjson

  • I wrote a testing code as the code below. The parsing result shows that there is no record in the query string. However there is one record in the string. Is there any problem with my code? Thanks beforehand.
    @#include <cstdlib>
    #include <iostream>
    #include <QtCore>
    #include <QtGui>
    #include <qjson/parser.h>

    using namespace std;

    pair<bool,QVariantList> parseJSON(QString path)
    QString query = "{"Result":"OK","TotalRecordCount":1,"Records":[{"id":"517b336a09ff1a15f3db1ecd","accountName":"admin","title":"2013","desc":"","status":0,"createTime":"2013-04-27 10:09:46","leaders":"admin"}]}";

    QJson::Parser parser;
    bool ok;
    QVariant result = parser.parse(query.toAscii(),&ok);
    if(ok) {
    QVariantList mylist = result.toList();
    return make_pair(true,mylist);
    } else
    return make_pair(false,QVariantList());

    int main(int argc,char ** argv)
    pair<bool,QVariantList> result = parseJSON("list.json");
    if(false == result.first) {
    cout<<"parse error!"<<endl;
    return EXIT_FAILURE;
    } else {
    QVariantList & mylist = result.second;
    foreach(QVariant plugin,mylist) {
    QVariantMap mymap = plugin.toMap();
    cout<<"id = "<<mymap["id"].toString().toStdString()<<endl
    <<"accountName = "<<mymap["accountName"].toString().toStdString()<<endl;
    return EXIT_SUCCESS;

  • Why do you expect "parser.parse(...)" to return a QVariantList? ... afaik, it would be a QVariantMap ...

    hmm ... or the other way around if you want the records-list, you first have to get it from the outer map:
    @QVariantList mylist = result.toMap()["Records"].toList()@
    (haven't tested that line, but that's what I would expect to work)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.