2

I have some UTF8 encoding errors using JSON in JAVA:

JSONObject json = new JSONObject();
json.put("Name", "Müller");
System.out.println(json.toString());

Output:

{"Name":"M\u00fcller"}

But I would like the following:

{"Name":"Müller"}

Any suggestions? Stromsam

4
  • have you set the character-set ? Commented Oct 31, 2011 at 11:03
  • 2
    Which JSON library are you using? Commented Oct 31, 2011 at 11:04
  • how can I set the charset for a JSONObject? Commented Oct 31, 2011 at 11:04
  • This is not an error, it's a correct Unicode string escape. You're far more likely to get a real encoding error writing a Java String to a stream. Commented Oct 31, 2011 at 11:13

1 Answer 1

1

There are a few bits of info which will make this a more useful question. firstly which JSON library are you using? are you using this in a standalone app or as part of a Java web-app?

if you are using the org.json.JSONObject.JSONObject() then what you have written should work.

The library org.json is available here json.org

running the below code with the suggested library :

JSONObject json = new JSONObject();
json.put("Name", "Müller");
System.out.println(json.toString());

produces :

{"Name":"Müller"}

seems to be what you want

hope that helps

Olly

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot! I used json-rpc-1.0.jar. With the libary from json.org it works the way I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.