Unicode characters displayed as Question Marks in Java JSON Parsing

I have been looking for this over the past few days, but I donโ€™t think I can find the right pointer. Combine it with the appropriate question if it is found as a duplicate.

I am new to working with JSON, and as part of one of my projects I need to decrypt the JSON file and continue processing it. However, when I tried to decode using the Json simple library, I get some weird question marks in the parsed object instead of the actual characters. A sample code is shown below:

String str = "{\"alias\": [\"Evr\u00f3pa\", \"\u05d0\u05d9\u05e8\u05d5\u05e4\"]}";
JSONParser parser = new JSONParser(); 
JSONObject jsonObject = (JSONObject)parser.parse(str);

System.out.println(jsonObject) gives {"alias":["Evrรณpa","?????"]}

I tried using Json-lib with the same result too.

Thanks for the help.

+5
source share
2 answers

JSON, System.out.println(). ( IDE, ) , System.out .

Unicode. , Unicode ( ) . . Unicode , ( System.out). Unicode UTF-8. - . , , debug println(), .

+7

, , . UTF-8 , - :

String str = "{\"alias\": [\"Evr\u00f3pa\", \"\u05d0\u05d9\u05e8\u05d5\u05e4\"]}";
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(str.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));
JSONParser parser = new JSONParser(); 
JSONObject jsonObject = (JSONObject)parser.parse(isr);
0

All Articles