0

In android I create JSON String like this

jsonObject.toString();

But the problem is that Unicode chars aren't encoded, for example:

{"number":123456,"name":"בית"}
Should be:
{"number":123456,"name":"\u05D1\u05D9\u05EA"}

How do I do that? I tried different JSON converting libs but couldn't one that does it. One solution that I found is to escape it using StringEscapeUtils, and remove double slash in the string, like this:

jsonArray.put("name", StringEscapeUtils.escapeJson(name));
jsonArray.toString().replace("\\\\u", "\\u");

But I don't think this is the best solution for this case..

Erez.info
  • 125
  • 1
  • 2
  • 11
  • `But I don't think this is the best solution for this case..`. Why? – Daniels Šatcs Oct 09 '16 at 14:07
  • i guess there won't be an elegant solution, furthermore unescaped Unicode chars are accepted by the JSON specification except _quotation mark (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F_ [spec-pdf](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) or you can try a library like this [answer](http://stackoverflow.com/a/13700590/2657100) – nandsito Oct 09 '16 at 15:59
  • Yes I know, it's just that I have a function that's causing me problems with the encoding, and this is solving it.. – Erez.info Oct 09 '16 at 19:25
  • Both forms are equivalent, and any proper JSON parser will return the same data when decoding either form. Just make sure you use UTF-8 or UTF-16 when reading or writing to a file. – roeland Oct 10 '16 at 03:38
  • I know, but I have problem in using UTF-8, and this is solving my issue. So how do I create JSON string like this in Android? – Erez.info Oct 10 '16 at 12:56

0 Answers0