Qt World Summit: Register Today!

Converting QByteArray to QString received from HTTPResponse

  • Hello,
    I am accessing an URL which gives me JSON content as below(Printed using qdebug) : This reply is got from QNetworkReply::readAll function

    {"message": "Success", "code": 200, "scene_label": {"Sequence": [{"SequenceDetails": {"FileName": "abc.h5", "FolderName": ""}, "Labels": [{"SourceType": "Image", "Devices": [{"Channels": [{"ChannelName": "LRR", "SceneLabels": {"GEN_LIGHT_CONDITIONS": [{"starttimestamp": "1558978895055520", "endtimestamp": "1558979211464202", "value": "Dawn"}], "GEN_COUNTRY": [{"starttimestamp": "1558978895055520", "endtimestamp": "1558979211464202", "value": "INDIA"}]}}], "DeviceName": "abc"}]}]}]}}

    But when I try to put in QString to write in file, the string is empty.

    Now again I tried to put in the file directly the bytes, still the same result.
    I suspect there are escape characters which may cause an issue.

    Please guide.


  • Lifetime Qt Champion


    Are you calling readAll several times ?

    If so, that's your issue. You're at the end of your IO device. The correct thing to do is to store the result of readAll in a variable and then use that one in your code.

    Note that if you want to write to a file, you don't even need to pass by QString. Just write the QByteArray directly.

    If not, then please show the code you are currently using.

  • Hi @SGaist .
    Thanks for the eye opener. Such a silly mistake.
    I called readAll() twice. 1st for printing and then again for storing into variable. So the second fetch is always empty.

Log in to reply