QByteArray into JSON
-
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:
-
Hi,
Take a look at the JSON Support in Qt chapter in Qt's documentation.
Can you show what it is supposed to contain ?
-
-
@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.
-
Can you show how you request your data ? It looks like you don't get ASCII in return.
-
@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());
-
@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
-
@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. -
You can use KArchive's KCompressionDevice to read GZip files: instead of
response->readAll()
you'll haveKCompressionDevice(response,false,KCompressionDevice::GZip).readAll()