Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse jsonarray?

Tags:

c++

json

qt

qt5

qtcore

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?

like image 662
user3490755 Avatar asked Apr 19 '14 19:04

user3490755


People also ask

How do I get items from JSONArray?

JSONArray jsonArray = (JSONArray) jsonObject. get("contact"); The iterator() method of the JSONArray class returns an Iterator object using which you can iterate the contents of the current array.

How can I turn a JSONArray into a JSON object?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

How do I pass JSONArray?

Solution: In order to pass the array value to the post action, you need to serialize the array value and pass the serialized data to the mapper action. In the post action, you can de-serialize the serialized data and use the data based on your requirement.


1 Answers

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 
like image 51
lpapp Avatar answered Sep 19 '22 21:09

lpapp