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 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à

    kshegunovK 1 Reply Last reply
    0
    • M Mark81

      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à

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      1
      • kshegunovK kshegunov

        @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 last edited by
        #3

        @kshegunov it outputs:

        "name":"citt?"

        kshegunovK 1 Reply Last reply
        0
        • M Mark81

          @kshegunov it outputs:

          "name":"citt?"

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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

          • Login

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