Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encoding and escaped strings
QtWS25 Last Chance

Encoding and escaped strings

Scheduled Pinned Locked Moved Unsolved General and Desktop
encodingutf-8qnetworkreply
4 Posts 2 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark81
    wrote on 25 May 2016, 10:42 last edited by
    #1

    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à

    K 1 Reply Last reply 25 May 2016, 18:58
    0
    • M Mark81
      25 May 2016, 10:42

      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à

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 25 May 2016, 18:58 last edited by kshegunov
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      M 1 Reply Last reply 26 May 2016, 06:56
      1
      • K kshegunov
        25 May 2016, 18:58

        @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.

        M Offline
        M Offline
        Mark81
        wrote on 26 May 2016, 06:56 last edited by
        #3

        @kshegunov it outputs:

        "name":"citt?"

        K 1 Reply Last reply 26 May 2016, 07:04
        0
        • M Mark81
          26 May 2016, 06:56

          @kshegunov it outputs:

          "name":"citt?"

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 26 May 2016, 07:04 last edited by kshegunov
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          2/4

          25 May 2016, 18:58

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved