1

In Java (using for Android), here are the code lines:

org.json.JSONObject array = new org.json.JSONObject("{\"value\":\"aész\"");
System.out.println("RES: " + array.toString());

The output I want:

RES: {"value":"a\u00e9sz"}

When it is actually:

RES: {"value":"aész"}

How do I make the JSONObject toString() method return the JSON String encoded with unicode values in the UTF-8 special characters, like the json_encode(array("value", "aész"));

Thanks in advance.

Grego
  • 2,220
  • 9
  • 43
  • 64
  • Can you talk more about *why* you want the output encoded that way? Both are valid JSON according to RFC 7159, mean the same thing, and should be decoded the same way. – Joe Hildebrand May 11 '14 at 06:21
  • it is because my third party API I'm using doesn't support non-english characters, so if it gets encoded, I can get the characters afterwards. – Grego May 11 '14 at 11:59

3 Answers3

2

You need to build your own version of the org.json.JSONObject class if you want it to escape all non-ASCII characters.

The signature of the method you need to modify is

 public static Writer quote(String string, Writer w) throws IOException

it is declared inside JSONObject.java. It is the method responsible of formatting all string values inside the produced json strings. it loops over all the characters of the source string and emits the corresponding output characters.

What you are looking for is in the "default" section of the switch statement.

the original code (at least in the sources I am watching right now) looks like this:

    default:
        if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
                || (c >= '\u2000' && c < '\u2100')) {
            w.write("\\u");
            hhhh = Integer.toHexString(c);
            w.write("0000", 0, 4 - hhhh.length());
            w.write(hhhh);
        } else {
            w.write(c);
        }

you need to change the "if" test to match all the characters you want to be escaped.

this does what you want:

    default:
        if (c < ' ' || c >= '\u0080') {
            w.write("\\u");
            hhhh = Integer.toHexString(c);
            w.write("0000", 0, 4 - hhhh.length());
            w.write(hhhh);
        } else {
            w.write(c);
        }

Hope this helps.

P.S: I run into your question because I met your same problem: the json strings I am generating need to travel through a system that accepts only ascii characters and mangles any character >127.

Carlo Sirna
  • 1,201
  • 6
  • 11
1

It sounds more like jar issue. JsonObject is the class used across various open source libraries. Download this jar json-rpc-1.0.jar

Try this:

JSONObject json = new JSONObject();
json.put("value", "aész");
System.out.println(json.toString());

produces :

{"Name":"u00e9sz"}
Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76
0

I suppose what you want is ISO-8859-1 encoding. Here is a good answer on that subject: Converting UTF-8 to ISO-8859-1 in Java - how to keep it as single byte

Community
  • 1
  • 1
enlait
  • 607
  • 1
  • 6
  • 19
  • hm, maybe I didn't understand how to implement in a JSON String, I gave a very small example, could you try to show with the example I gave? – Grego May 09 '14 at 13:28