Qt Json Demo

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QByteArray>

int main(int argc, char *argv[])
{
        QApplication a(argc, argv);
        MainWindow w;
        w.show();

        const char* json = "[{\"id\":1, \"file\":\"f\"}, {\"id\":2, \"file\":\"ff\"}]";
        QJsonDocument doc = QJsonDocument::fromJson( json );

        qDebug() << "isArray:" << doc.isArray() << endl;
        QJsonArray ja = doc.array();
        QJsonArray::Iterator it = ja.begin();

        QJsonObject o;

        while ( it != ja.end() ) {
                o = (*it).toObject();
                qDebug() << "id:" << o.value( "id" ).toDouble() << endl;
                ++it;
        }

        return a.exec();
}

输出

id: 1 

id: 2

 

posted @ 2013-11-22 11:00  Leo Forest  阅读(654)  评论(0编辑  收藏  举报