Important: Please read the Qt Code of Conduct -

Create QByteArray JSON with special characters (é, ...) as unicode (\uxxxx)

  • I would like to create a simple JSON structure like follow :

    { "é" : 1 }

    And to finally get it as a valid JSON QByteArray.

    Here is my code in order to achieve this :

    QJsonObject data;
    data.insert("é", 1);
    QJsonDocument jsonDoc(data);
    QByteArray bjsonData = jsonDoc.toJson();

    But here the "é" is stored as "\xC3\xA9", it is the UTF-8 representation of the character but I would like to get "\u00e9" instead (the unicode code point) as specified by the JSON RFC.

    Is there a way to get escaped Unicode value instead of escaped utf-8 value ?

  • It is not escaped.
    It is just the QDebug output of unprintable char.
    If you convert the QByteArray to a QString with QString::fromUtf8(), it is still a "é".

  • @Bonnie said in Create QByteArray JSON with special characters (é, ...) as unicode (\uxxxx):


    Ahh fine, thanks for the reply.

    And so is there a way to get the Unicode escaped format ?

  • @Mixlu
    What do you mean by "Unicode escaped format"?
    Normal characters won't be escaped, even a character like é also doesn't need to be escaped.
    If you use some special characters like \0, then it will be escaped to\u0000