I have a JSON but all cyrillic characters are encoded like this: \u0418\u0437\u0431\u0435
.
How can I read this into QString?
2 Answers
That's JSON's Unicode escape sequence. You should use a JSON parser for this. There are some other gotchas in JSON parsing.
If you are using Qt 5, it comes with one: http://qt-project.org/doc/qt-5.0/qjsondocument.html
If you are not, then get a 3rd party one like jsoncpp. It's rather lightweight and not to difficult to convert between its data and Qt's.
That's should be an UTF8 or UTF16 encoding. Use this:
result = QString::fromUtf8(yourString.c_str(), yourString.size());
or
result = QString::fromUtf16(yourString.c_str(), yourString.size());
where yourString is std::string type
-
it doesn't works. Actually I'm receiving the data in QByteArray. Do you have any other idea? Commented Nov 14, 2012 at 18:20
-
define your "doesn't work". Does it compile? Run time error? Make an example of what string you give and what string you get (explain if you can't print non-ASCII character in stackoverflow.com) Commented Nov 15, 2012 at 8:08
-
it doesn't convert it - it returns the same sequence. I have Json containing escaped Cyrillic characters
\u0418\u0437\u0431\u0435
. When I useresult = QString::fromUtf8(yourString.c_str(), yourString.size());
in result I get the same sequence, not the Cyrillic characters. What to do? Commented Nov 15, 2012 at 8:32