Nominate our 2022 Qt Champions!

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;


    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:


    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:


    I'm expecting:


  • Moderators

    What does this print out:

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

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

  • @kshegunov it outputs:


  • Moderators

    Curious. What about:

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

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

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