mardi 21 avril 2015

how to parse json with qt quick?

I am new to QT quick. I am trying to do a simple programming to parse JSON message value from web to QT quick. I have do the study and refer some codes from internet. However, I hit a but that make me can't go on.

The JSON message would like to parse:

      "id": 2,
      "name": "yahoo",
      "link": "",
      "created_by": "superadmin",
      "create_time": "2015-03-12 14:45:21"
      "id": 3,
      "name": "Google",
      "link": "",
      "created_by": "superadmin",
      "create_time": "2015-03-12 14:45:41"
      "id": 4,
      "name": "PS",
      "link": "",
      "created_by": "superadmin",
      "create_time": "2015-03-24 11:57:21"

The following is the QT code to try to parse these data: #include #include #include #include #include #include #include #include #include #include #include #include

               This example illustrates exposing a QStringList as a
               model in QML

            class Jsondata {
                QStringList datalist;
                void onResult (QNetworkReply*);
                QStringList datalistmethod() {
                    return datalist;

            void Jsondata::onResult(QNetworkReply* reply)
                //if (m_currentReply->error() != QNetworkReply::NoError)
                  //  return;  // ...only in a blog post

                QString data = (QString) reply->readAll();

                QScriptEngine engine;
                QScriptValue result = engine.evaluate(data);

                // Now parse this JSON according to your needs !
                QNetworkAccessManager networkManager;

                QUrl url("http://***/api/web/v1/links");
                QNetworkRequest request;

                QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));
                networkManager.get(request);  // GET

                QScriptValue entries = result;
                QScriptValueIterator it(entries);
                while (it.hasNext()) {
                    QScriptValue entry = it.value();

                    QString name ="name").toString();
                    QString link ="link").toString();


            int main(int argc, char ** argv)
                QGuiApplication app(argc, argv);

                Jsondata jsondata;

                QStringList datalist;
                datalist = jsondata.datalistmethod();

                QQuickView view;
                QQmlContext *ctxt = view.rootContext();
                ctxt->setContextProperty("myModel", QVariant::fromValue(datalist));


                return app.exec();

There are error: candidate function not viable: no known conversion from 'Jsondata *' to 'const QObject *' for 3rd argument static QMetaObject::Connection connect(const QObject *sender, const char *signal

I don't have knowledge on this Qobject::connect and don't know what wrong. I would appreciate it if you could help me to fix this bug. Thank you.

Warm regards, Lapson

Aucun commentaire:

Enregistrer un commentaire