Java map, how to put a UTF-8 string on a map correctly?

I have a Map, LinkedHashMap, to be more precise. And I want to put a string object in it. And then I read this value to see what is actually stored. The string itself has non-ascii characters (Cyrillic, Korean, etc.). As soon as I put it on the card and then read it, these characters are replaced by. Some code:

Map obj = new LinkedHashMap();
System.out.println("name: " + getName());  // prints "i4niac__sim"
obj.put("name", getName());
System.out.println("written stuff: " + obj.get("name"));  // prints i4niac_???_sim

What is the trick here? I use this map to make a JSON object with json-simple and send it from the server to the client.

Update:

Wow, sorry for the whole mess. At first I blamed the datastore, and then the map, finally, as expected, it was my mistake elsewhere. I sent json data in the application settings content type as "application / json"

public void doPost(HttpServletRequest req, HttpServletResponse resp) {
// ...
        resp.setContentType("application/json");
        resp.getWriter().println(jsonObj.toString());
}

UTF-8, , .

    resp.setCharacterEncoding("UTF-8");

, UTF-8 , ascii.

+3
3

, ??? - , ASCII ? , , , UTF-8? - MS Word , ?

0

-encoding

javac -encoding UTF-8 Test3.java
0

Java, UTF-8 ?

UTF-16. , .

LinkedHashMap , , , .

The only explanations I can think of are:

  • getName() does not return a link to the same line every time (most likely)
  • System.out PrintWriter changes simultaneously
  • The coding of the console receiving the data changes simultaneously

You can emit the hexadecimal form of String to make sure display errors are not a problem:

public static String toCodeUnits(String s) {
  StringBuilder sb = new StringBuilder();
  for(char codeUnit : s.toCharArray()) {
    sb.append(String.format("%04x ", (int) codeUnit));
  }
  return sb.toString();
}

For i4niac__simthis code will return:

"0069 0034 006e 0069 0061 0063 005f 0441 0438 043c 005f 0073 0069 006d "
0
source

All Articles