Encoding and escaped strings



  • Hello,
    after sending a request to a remote service, QNetworkReply returns an encoded string, i.e.:

    QByteArray data = reply->readAll();
    qDebug() << data;
    

    "name":"citt\xC3\xA0"

    I need to pass this string (it's a json) to QJsonDocument. Usually I do:

    QJsonDocument doc = QJsonDocument::fromJson(data);
    

    But now the output of the name field is:

    citt?

    I'm quite confused about encoding and escaping.
    I tried the following:

    1. convert the raw (escaped) data to UTF-8
    2. convert the QString to QByteArray to create the JSON doc
    QByteArray raw = reply->readAll();
    QJsonDocument doc = QJsonDocument::fromJson(QString::fromUtf8(raw).toUtf8());
    qDebug() << doc.toJson();
    

    anyway the output is still:

    citt?

    I'm expecting:

    città


  • Qt Champions 2016

    @Mark81
    Hello,
    What does this print out:

    QTextStream out(stdout);
    out << QString::fromUtf8(raw) << endl;
    

    PS.
    qDebug() will escape non-latin and/or non-text characters.



  • @kshegunov it outputs:

    "name":"citt?"


  • Qt Champions 2016

    @Mark81
    Curious. What about:

    qDebug() << (QString::fromUtf8(value) == QStringLiteral("citt\u00E0"));
    

    Where value is the JSON value of that particular field (i.e. città).

    PS.
    What I'm trying to figure out is if this is an output issue, or encoding issue.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.