Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. format JSON

format JSON

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RodrigoFBM
    wrote on last edited by RodrigoFBM
    #1

    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;
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean something like described here ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RodrigoFBM
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RodrigoFBM
            wrote on last edited by RodrigoFBM
            #5
                            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • R Offline
                R Offline
                RodrigoFBM
                wrote on last edited by RodrigoFBM
                #7

                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?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Why the event loop ?

                  You can do the processing once your reply is finished.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • R Offline
                    R Offline
                    RodrigoFBM
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      What did not work ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved