Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    The problem of parsing json with libqjson

    3rd Party Software
    2
    2
    1702
    Loading More Posts
    • 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
      breadbread1984 last edited by

      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 Reply Quote 0
      • S
        sl.sy.ifm last edited by

        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 Reply Quote 0
        • First post
          Last post