I tried to use Gson and org.json as the examples. I tried Commons Text as well, but I doesn't work for me, when I import the library manually (I not allowed to use Maven). So I decided to search for another solution.
NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
I need to escape an Array to Json this way. Especially Ó
, ó
or any Latin-1 character( Not escape "
, just escape what is in "&%$/"Helló"
). Original message: Helló / // \ "WÓRLD"
{"token":"045-245","message":"Helló / // \\ \"WÓRLD\" "}
to
{"token":"045-245","message":"Hell\u00F3 / // \\ \"W\u00D3RLD\" "}
This what I get when I use:
Gson
JsonObject json = new JsonObject();
json.addProperty("token", "045-245");
json.addProperty("message", "Helló WÓRLD");
String payload = new Gson().toJson(json);
System.out.println(payload);
Result:
{"token":"045-245","message":"Helló WÓRLD"}
org.json
JSONObject jsonObject = new JSONObject();
jsonObject.put("token", "045-245");
jsonObject.put("message", "Helló WÓRLD");
System.out.println(jsonObject.toString());
Result:
{"message":"Helló WÓRLD","token":"045-245"}