解析jsonarray吗? [英] Parse jsonarray?

查看:169
本文介绍了解析jsonarray吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下内容的JSON:

{
    "agentsArray": [{
        "ID": 570,
        "picture": "03803.png",
        "name": "Bob"
    }, {
        "ID": 571,
        "picture": "02103.png",
        "name": "Tina"
    }]
}

现在,我试图遍历每个数组元素.使用qt-json https://github.com/da4c30ff/qt-json

尝试:

            foreach(QVariantMap plugin, result["agentsArray"].toList()) {
                qDebug() << "  -" << plugin["ID"].toString();
            }

但是无法使它正常工作,有什么想法我做错了吗?

解决方案

我建议在Qt 5中使用QtCore的QJson *类.由于针对读取和写入进行了优化的机器可读二进制存储,因此它们非常有效.由于它们具有出色的API,因此使用它们也非常方便.

此代码库对我来说很好,但是请注意,我暂时忽略了所有错误检查,这对于生产代码不是一个好的建议.这分别只是一个原型代码.

main.json

{
    "agentsArray": [{
        "ID": 570,
        "picture": "03803.png",
        "name": "Bob"
    }, {
        "ID": 571,
        "picture": "02103.png",
        "name": "Tina"
    }]
}

main.cpp

#include <QFile>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

int main()
{
    QFile file("main.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray jsonData = file.readAll();
    file.close();

    QJsonDocument document = QJsonDocument::fromJson(jsonData);
    QJsonObject object = document.object();

    QJsonValue value = object.value("agentsArray");
    QJsonArray array = value.toArray();
    foreach (const QJsonValue & v, array)
        qDebug() << v.toObject().value("ID").toInt();

    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

构建并运行

qmake && make && ./main

输出

570 
571 

I've got an JSON like the following:

{
    "agentsArray": [{
        "ID": 570,
        "picture": "03803.png",
        "name": "Bob"
    }, {
        "ID": 571,
        "picture": "02103.png",
        "name": "Tina"
    }]
}

Now I'm trying to loop through each array element. Using the qt-json library https://github.com/da4c30ff/qt-json

Tried:

            foreach(QVariantMap plugin, result["agentsArray"].toList()) {
                qDebug() << "  -" << plugin["ID"].toString();
            }

But cannot get it to work, any ideas what I'm doing wrong?

解决方案

I would recommend using the QJson* classes from QtCore in Qt 5. They are very efficient due to the machine readable binary storage optimized for reading and writing, and it is also very convenient to use them due to the nice API they have.

This code base works for me just fine, but please note that I neglected all the error checking for now which is not a good advice for production code. This is just a prototype code, respectively.

main.json

{
    "agentsArray": [{
        "ID": 570,
        "picture": "03803.png",
        "name": "Bob"
    }, {
        "ID": 571,
        "picture": "02103.png",
        "name": "Tina"
    }]
}

main.cpp

#include <QFile>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

int main()
{
    QFile file("main.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray jsonData = file.readAll();
    file.close();

    QJsonDocument document = QJsonDocument::fromJson(jsonData);
    QJsonObject object = document.object();

    QJsonValue value = object.value("agentsArray");
    QJsonArray array = value.toArray();
    foreach (const QJsonValue & v, array)
        qDebug() << v.toObject().value("ID").toInt();

    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

570 
571 

这篇关于解析jsonarray吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆