如何为Qt Quick解析JSON?

时间:2015-04-21 08:02:36

标签: c++ json qt qt5 qt-quick

我正在尝试进行简单的编程,将JSON消息值从Web解析为Qt Quick。

JSON消息想解析:

[
    {
         "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://www.playstation.com",
         "created_by": "superadmin",
         "create_time": "2015-03-24 11:57:21"
    }
]

以下是尝试解析这些数据的Qt代码main.cpp:

#include <QGuiApplication>
#include <QStringList>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValueIterator>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <main.h>



QStringList Jsondata::datalistmethod() {

   return datalist;
}


void Jsondata::onResult(QNetworkReply* reply)
{

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


    QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8());
    QJsonObject jsonObject = jsonResponse.object();


    foreach (const QJsonValue & value, jsonObject) {
        QJsonObject obj = value.toObject();
        datalist.append(obj["name"].toString());
        datalist.append(obj["link"].toString());
    }
}

int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
    Jsondata jsondata;
    // Now parse this JSON according to your needs !
   QNetworkAccessManager networkManager;
   networkManager.setNetworkAccessible(QNetworkAccessManager::Accessible);

   QUrl url("http://dbs.wasonicsystem.com/gta_test/api/web/v1/links");
   QNetworkRequest request;
   request.setUrl(url);

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

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

main.h

#ifndef MAIN
#define MAIN
#include <QObject>
#include <QNetworkReply>
#include <QStringList>
class Jsondata : public QObject {
    Q_OBJECT
    QStringList datalist;
public slots:
    void onResult (QNetworkReply*);
public:
    QStringList datalistmethod();
};
#endif // MAIN

main.qml

import QtQuick 2.0
//![0]

ListView {
    width: 100; height: 100

    model: myModel
    delegate: Rectangle {
        height: 25
        width: 100
        Text { text: modelData }
    }
}
//![0]

结果: onResult仍未调用。所以没有数据主义者返回

我不了解QObject::connect并且不知道错误。如果你能帮助我解决这个问题,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我终于找到了解决问题的方法。以下是代码段。

的main.cpp

#include <QGuiApplication>
#include <QStringList>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <jsondata.h>

int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);
    QStringList datalist;
    Jsondata jsondata;
    jsondata.datalistmethod();
    datalist = jsondata.datalist;

    QQuickView view;
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(datalist));
    view.setSource(QUrl("qrc:main.qml"));
    view.show();

    return app.exec();

}

jsondata.h

#ifndef JSONDATA_H
#define JSONDATA_H
#include <QObject>
#include <QNetworkReply>
#include <QStringList>
#include <QNetworkAccessManager>
class Jsondata : public QObject {
    Q_OBJECT
public:
    QStringList datalist;
    explicit Jsondata(QObject *parent =0);
    void datalistmethod();
public slots:
    void onResult (QNetworkReply*);

private:
    QNetworkAccessManager *manager;
};

jsondata.cpp

#include "jsondata.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QEventLoop>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <QTimer>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>

Jsondata::Jsondata(QObject *parent) :
    QObject(parent)
{

}


void Jsondata::datalistmethod() {
    // Now parse this JSON according to your needs !
   manager = new QNetworkAccessManager(this);

   manager->setNetworkAccessible(QNetworkAccessManager::Accessible);
    QNetworkRequest request;
   QEventLoop eventloop;
   QUrl url("http://***/api/web/v1/links");
    request.setUrl(url);
     QNetworkReply *reply = manager->get(request);
   connect(reply, SIGNAL(finished()), &eventloop, SLOT(quit()));
   eventloop.exec();
   onResult(reply);

}


void Jsondata::onResult(QNetworkReply* reply)
{
    QString data = (QString) reply->readAll();
    qDebug() << "Response:" << data;
    QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8());
    QJsonArray jsonArray = jsonResponse.array();
    foreach (const QJsonValue & value, jsonArray) {
        QJsonObject obj = value.toObject();
        datalist.append(obj["name"].toString());
        datalist.append(obj["link"].toString());
    }
}

通过添加事件循环,datalist方法将等待onResult方法完成以执行下一行代码。这是使用字符串列表模型的示例。如果有人觉得它对参考有用,请为我的帖子+1。谢谢。 ^^