Qt World Summit: Submit your Presentation

JSON response string to QNetworkAccessManager

  • I am pulling data from a remote database via a php script with http get requests. I am using the standard approach, creating a QNetworkAccessManager and connecting the reply to a reply handler to interpret the ensuing QNetwork reply object pointer. I can successfully receive various data types from the database without any problems.

    Some of the types are json encode strings, in which case I interpret it with:

    void responseHandler( QNetworkReply* response ) {
        QString responseString = response->readAll(); 
        QJsonDocument doc = QJsonDocument::fromJson( responseString.toUtf8());

    This work well for about 10 different json data sets. For one particular data set, Qt seems to be manipulating the string before
    my reply hander gets it.

    The php function is as follows:

        static function http_response_json( $text, $req_id=0 ) { 
            M::log( "($req_id) Sending text (" . strlen($text) . "): $text" );
            header('Content-Type: text/json');                 # its a text     file
            header('Content-Length: ' . strlen($text));
            print $text;

    According to my php script, it is sending the following text:

    {"rfv_id":"4","rf_filename":"test2","rfv_file":"{\"reaction-set-type\":\"linear\",\"reactions\":[{\"data\":[{\"time\":0,\"voltage\":10},{\"t      ime\":0.0012,\"voltage\":4.6},{\"time\":0.0041,\"voltage\":4.6},{}]}]}","rfv_file_crc32":"241367308","hwdv_name":"No hardware","cprj_name":"Mucking around","ccust_name":"Add Haptics"}

    An online json validator validates the string as valid json notation.

    The response string of the qnetworkreply object as received (while debugging) by the response handler is:

    array(7) {\n  [\"rfv_id\"]=>\n  string(1) \"4\"\n  [\"rf_filename\"]=>\n  string(5) \"test2\"\n  [\"rfv_file\"]=>\n  string(142) \"{\"reaction-set-type\":\"linear\",\"reactions\":[{\"data\":[{\"time\":0,\"voltage\":10},{\"time\":0.0012,\"voltage\":4.6},{\"time\":0.0041,\"voltage\":4.6},{}]}]}\"\n  [\"rfv_file_crc32\"]=>\n  string(9) \"241367308\"\n  [\"hwdv_name\"]=>\n  string(11) \"No hardware\"\n  [\"cprj_name\"]=>\n  string(14) \"Mucking around\"\n  [\"ccust_name\"]=>\n  string(11) \"Add Haptics\"\n}\nbool(true)\n{\"rfv_id\":\"4\",\"rf_filename\":\"test2\",\"rfv_file\":\"{\\\"reaction-set-type\\\":\\\"linear\\\",\\\"reactions\\\":[{\\\"data\\\":[{\\\"time\\\":0,\\\"voltage\\\":10},{\\\"time\\\":0.0012,\\\"voltage\\\":4.6},{\\\"time\\\":0.0041,\\\"voltage\\\":4.6},{}]}]}\",\"rfv_file_crc32\":\"241367308\",\"hwdv_name\":\"No hardware\",\"cprj_name\":\"Mucking around\",\"ccust_name\":\"Add Haptics\"}"

    which is obviously not what my php script sent and no longer a valid json object. I suspect is Qt adding the array() and slashes before the quotes in this particular case while not in most others. Assuming that this is the case, how do I get Qt not to do that anymore?

    Thanks in advance for any ideas!

  • Lifetime Qt Champion

    Apart from the fat that response->readAll() already returns a QByteArray so the (wrong) conversion to a QString and back is useless, Qt does for sure not modify your string under the hood. It's most likely your script. You can check e.g. with wireshark what is really send through the line

  • Thanks for the feedback! It's what I expected but it was good to have the confirmation.

    With a fresh start after the weekend, I found a var_dump in my php script which was sending its output to my Qt application. I usually avoid var_export to avoid that sort of error.

    Thanks for catching the QByteArray -> QString -> QByteArray which is unnecessary and doesn't help performance, although I don't know if I would not strictly speaking call it wrong. My implementation now uses a QByteArray class variable in most places where a QJsonObject is the expected output.

Log in to reply