Search code examples
javajsoncharacter-encoding

JSON UTF-8 encoding errors


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


Solution

  • 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