Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. The problem of parsing json with libqjson
QtWS25 Last Chance

The problem of parsing json with libqjson

Scheduled Pinned Locked Moved 3rd Party Software
2 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    breadbread1984
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sl.sy.ifm
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved