format JSON



  • alt text

    How I can pass for the textEdit something like:

    Nome: Rodrigo
    E-mail: ...
    
    Nome: Diego
    E-mail: ...
    

    and not like in the img?
    I've looked in some places for an example but nothing helped me.

    My code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkReply>
    #include <QtNetwork/QNetworkRequest>
    #include <QUrl>
    #include <QUrlQuery>
    #include <QMessageBox>
    #include <QObject>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>
    #include <QEvent>
    #include <QDebug>
    #include <QList>
    #include <QString>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
        QEventLoop eventLoop;
            QNetworkAccessManager manager;
            QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
            QNetworkRequest req( QUrl( QString("http://localhost:3000/api/v1/contacts") ) );
            req.setRawHeader("X-User-Email", "rore@hotmail.com");
            req.setRawHeader("X-User-Token", "iHik3GFTy9xNrJotdsCb");
    
            QNetworkReply *reply = manager.get(req);
            eventLoop.exec();
    
            QString response = (QString)reply->readAll();
    
            //qDebug() << "Custom coordinate type:" << response;
    
            if (reply->error() == QNetworkReply::NoError) {
                    QJsonDocument jsonResponse = QJsonDocument::fromJson(response.toUtf8());
                    QJsonObject jsonObj = jsonResponse.object();
                    QByteArray jsonString = jsonResponse.toJson(QJsonDocument::Indented);
                   // qDebug() << "Custom coordinate type:" << jsonString;
                    ui->textEdit->setText(jsonString);
    
                    delete reply;
                }
            else {
               QMessageBox::critical(this, "ERROR:", reply->errorString());
                delete reply;
            }
    
       
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Do you mean something like described here ?



  • Hi. I think so. But in my Json I don't have something like

    [
      **"properties":**
        {
            "created_at": "2018-01-08T22:10:32.235Z",
            "description": null,
            "email": "",
            "id": 1,
            "name": "Rodrigo",
            "phone": null,
            "updated_at": "2018-01-08T22:10:32.235Z",
            "user_id": 1
        },
        {
            "created_at": "2018-01-08T22:11:09.573Z",
            "description": null,
            "email": "",
            "id": 2,
            "name": "Diego",
            "phone": null,
            "updated_at": "2018-01-08T22:11:09.573Z",
            "user_id": 1
        }
    ]
    

    for pass in jsonObject index like: .

    QJsonArray jsonArray = jsonObject["properties"].toArray();
    

    How I fix that?


  • Lifetime Qt Champion

    You have an array of dict. So extract that directly rather than an object.



  •                 QStringList names;
                    QJsonDocument jsonResponse = QJsonDocument::fromJson(response.toUtf8());
                    QJsonObject jsonObj = jsonResponse.object();
                    QByteArray jsonArray = jsonResponse.toJson(QJsonDocument::Indented);
    
                    foreach (const QJsonValue &value, jsonArray) {
                        QJsonObject obj = value.toObject();
                        names.append(obj["name"].toString());
    
                    }
                     qDebug() <<names[0];
    

    I tried it, but on debug console return just " ". What I did wrong?


  • Lifetime Qt Champion

    Why are you create a QByteArray ? You need a QJsonArray, see QJsonDocument::array.



  • Yeah! It wroks! Thanks!

    So...

            QEventLoop eventLoop;
            QNetworkAccessManager manager;
            QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
            QNetworkRequest req( QUrl( QString("http://localhost:3000/api/v1/contacts") ) );
            req.setRawHeader("X-User-Email", "rodrigo_fbm@hotmail.com");
            req.setRawHeader("X-User-Token", "iHik3GFTy9xNrJotdsCb");
    

    Is the most appropriate way to connect? If not, what is?


  • Lifetime Qt Champion

    Why the event loop ?

    You can do the processing once your reply is finished.



  • I looked up some examples on Google, and they were like that. I looked for something more current, but it did not work. In this case, what should I do?


  • Lifetime Qt Champion

    What did not work ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.