I have to pass unicode string to a JSONObject.
JSONObject json = new JSONObject("{\"One\":\"\\ud83c\\udf45\\ud83c\\udf46\"}");
json.put("Two", "\ud83c\udf45\ud83c\udf46");
System.out.println(json.toString());
but I have this:
{"One":"","Two":""}
I want this:
{"One":"\ud83c\udf45\ud83c\udf46","Two":"\ud83c\udf45\ud83c\udf46"}
The system is working as designed. You are just not taking into account that JSON does not require most Unicode characters to be formatted in
\uXXXX
format. Certain escape characters must be in\X
format, and control characters <= 0x1F must be in\uXXXX
format, but any other character may be in\uXXXX
format but is not required to be. The characters you have shown do not fall into those ranges, which is whytoString()
is not encoding them in\uXXXX
format.When you call
new JSONObject(String)
, it decodes the input string into actual Unicode strings, as if you had done this instead:Which is perfectly fine. You want the
JSONObject
to hold un-escaped Unicode data internally.Where you are getting tripped up is the fact that
JSONObject.toString()
is not formatting your particular Unicode characters in\uXXXX
format. That is perfectly valid JSON, but is not how you are wanting them to be formatted (why do you want them formatted this way?).A look at the source for Java's
JSONStringer
class (which implementsJSONObject.toString()
) reveals that it only formats non-reserved control characters <= 0x1F in\uXXXX
format, other non-reserved characters are formatted as-is. This conforms to the JSON specification.To do what you are asking for, you will have to manually format Unicode characters as needed after calling
JSONObject.toString()
to format reserved and ASCII characters normally, eg: