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. QByteArray into JSON

QByteArray into JSON

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 14.5k Views
  • 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.
  • T Offline
    T Offline
    t0msk
    wrote on 10 Jul 2018, 20:56 last edited by
    #1

    Hello,

    I get from QNetworkReply this:

    "\x1F\x8B\b\x00\x00\x00\x00\x00\x02\xFF\xAD\x90[\n\xC2""0\x10""E\xB7R\xF2\xAD&\x93""4}mF&\x93)\r\xB6MIZ\x8B\x88{WAp\x01\xFA{?\xCE""9\xDC\xBBX8M8\x86\xF9\"\xBA""B\f\xEB\xBA\xE4N\xCA}\xDFO\xD7\x90\xB6\xBC\xC6\x15\xC7\x13\xC5I\xF6""adI\rW\xC0H\xCEy\x03\xC8\x16\xD8*T\xAA""5lK\xDD:W9F\xD5\x97\xBE\xD1\x9At\xDD\x96\xE4mC\xAE\x87\x9AMe\x1A\x02\x89""3\x8E\xB7\x1C\xB2\x04k@\x1B\x05\xA0\xA4""8\x14\"q^\xE2\x9C\xF9L\xD1\xF3\xAB\x04^[\x1EP\xDB\xEA\x9D\xF5\xAB\xF6""c\x88[\"\xFE\x17/\x13\xCE\xE7\xE0\xFF\x81;~\xCF\x10\x8F'\n4\xB5V\x92\x01\x00\x00"
    

    And it should be JSON, but it is in QByteArray and I need to convert it into JSON, how can I do it?

    I tried this:

    QByteArray reply = response->readAll();
    reply.data();
    

    And output from it is:
    Output

    Student who loves C/C++

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Jul 2018, 21:01 last edited by
      #2

      Hi,

      Take a look at the JSON Support in Qt chapter in Qt's documentation.

      Can you show what it is supposed to contain ?

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

      T 1 Reply Last reply 10 Jul 2018, 21:11
      2
      • O Offline
        O Offline
        ODБOï
        wrote on 10 Jul 2018, 21:04 last edited by
        #3

        hi,
        https://forum.qt.io/topic/69728/can-t-get-json-file-from-qnetworkreply/2

        1 Reply Last reply
        0
        • S SGaist
          10 Jul 2018, 21:01

          Hi,

          Take a look at the JSON Support in Qt chapter in Qt's documentation.

          Can you show what it is supposed to contain ?

          T Offline
          T Offline
          t0msk
          wrote on 10 Jul 2018, 21:11 last edited by t0msk 7 Oct 2018, 21:12
          #4

          @SGaist It should contain something like:

          {
            'response': 1,
            'scan': '123',
            'sha256': '54bc950d46a0d1aa71048a17c8275743209e6c17bdacfc5cb9601c9ce3ec9a71',
            'resource': '7647fcb7d772441a6d8504e4b21168b8',
          }
          

          @LeLev It doesn't help me, I have correct headers and I tried:

          QByteArray reply = response->readAll();
          QJsonDocument document = QJsonDocument::fromJson(response->readAll());
          QJsonObject rootObj = document.object();
          

          But in debugger I don't see any data in rootObj.

          Student who loves C/C++

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 10 Jul 2018, 22:07 last edited by
            #5

            Can you show how you request your data ? It looks like you don't get ASCII in return.

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

            T 1 Reply Last reply 10 Jul 2018, 22:34
            0
            • S SGaist
              10 Jul 2018, 22:07

              Can you show how you request your data ? It looks like you don't get ASCII in return.

              T Offline
              T Offline
              t0msk
              wrote on 10 Jul 2018, 22:34 last edited by
              #6

              @SGaist Of course, here it is:

              QUrl url("www.example.com");
              QUrlQuery query;
              
              query.addQueryItem("apikey", "123");
              query.addQueryItem("source", "123456");
              
              url.setQuery(query.query());
              
              QNetworkRequest request(url);
              
              request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
              request.setRawHeader("Accept-Encoding", "gzip, deflate");
              request.setRawHeader("User-Agent", "gzip,  Test app");
              
              QNetworkAccessManager *manager = new QNetworkAccessManager(this);
              
              QObject::connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::replyFinished);
              manager->post(request, query.toString(QUrl::FullyEncoded).toUtf8());
              

              Student who loves C/C++

              J 1 Reply Last reply 11 Jul 2018, 04:23
              0
              • T t0msk
                10 Jul 2018, 22:34

                @SGaist Of course, here it is:

                QUrl url("www.example.com");
                QUrlQuery query;
                
                query.addQueryItem("apikey", "123");
                query.addQueryItem("source", "123456");
                
                url.setQuery(query.query());
                
                QNetworkRequest request(url);
                
                request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
                request.setRawHeader("Accept-Encoding", "gzip, deflate");
                request.setRawHeader("User-Agent", "gzip,  Test app");
                
                QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                
                QObject::connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::replyFinished);
                manager->post(request, query.toString(QUrl::FullyEncoded).toUtf8());
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 11 Jul 2018, 04:23 last edited by
                #7

                @t0msk You get (because you request it) gzip encoded data (see https://stackoverflow.com/questions/6059302/how-to-check-if-a-file-is-gzip-compressed).

                request.setRawHeader("Accept-Encoding", "gzip, deflate");
                

                You need to uncompress it first. See http://doc.qt.io/qt-5/qbytearray.html#qUncompress

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply 11 Jul 2018, 08:06
                3
                • J jsulm
                  11 Jul 2018, 04:23

                  @t0msk You get (because you request it) gzip encoded data (see https://stackoverflow.com/questions/6059302/how-to-check-if-a-file-is-gzip-compressed).

                  request.setRawHeader("Accept-Encoding", "gzip, deflate");
                  

                  You need to uncompress it first. See http://doc.qt.io/qt-5/qbytearray.html#qUncompress

                  T Offline
                  T Offline
                  t0msk
                  wrote on 11 Jul 2018, 08:06 last edited by
                  #8

                  @jsulm So I changed it to:

                  QByteArray reply = qUncompress(response->readAll());
                  qDebug() << reply;
                  

                  And I get:

                  qUncompress: Z_DATA_ERROR: Input data is corrupted
                  ""
                  

                  Student who loves C/C++

                  J 1 Reply Last reply 11 Jul 2018, 08:13
                  0
                  • T t0msk
                    11 Jul 2018, 08:06

                    @jsulm So I changed it to:

                    QByteArray reply = qUncompress(response->readAll());
                    qDebug() << reply;
                    

                    And I get:

                    qUncompress: Z_DATA_ERROR: Input data is corrupted
                    ""
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Jul 2018, 08:13 last edited by
                    #9

                    @t0msk
                    https://stackoverflow.com/questions/2690328/qt-quncompress-gzip-data deals with this.

                    TL;DR:

                    qUncompress uses the zlib uncompress, so it can only handle the zlib format, not the gzip format.
                    you'll need to write your own to functions to handle gzip data.

                    1 Reply Last reply
                    3
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 11 Jul 2018, 08:31 last edited by
                      #10

                      You can use KArchive's KCompressionDevice to read GZip files: instead of response->readAll() you'll have KCompressionDevice(response,false,KCompressionDevice::GZip).readAll()

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      5

                      4/10

                      10 Jul 2018, 21:11

                      • Login

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