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": "http://www.yahoo.com/",
      "created_by": "superadmin",
      "create_time": "2015-03-12 14:45:21"
    },
    {
      "id": 3,
      "name": "Google",
      "link": "http://www.google.com/",
      "created_by": "superadmin",
      "create_time": "2015-03-12 14:45:41"
    },
    {
      "id": 4,
      "name": "PS",
      "link": "http://ift.tt/SdS6xU",
      "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;
            public:
                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;
                networkManager.setNetworkAccessible(QNetworkAccessManager::Accessible);

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

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

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

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

                    datalist.append(name);
                    datalist.append(link);
                }
            }

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

                view.setSource(QUrl("qrc:main.qml"));
                view.show();

                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