I have the below code in my program:
BufferedReader br = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
Object obj = null;
try {
obj = JSONValue.parse(br);
} catch (Exception e) {
System.out.println(e);
}
response = obj.toString();
The problem is that it's not converting chars like \u... to normal chars. For example i get:
caffË instead of caffè felicit\u2021 instead of felicità
I'm actually using the json-simple api: http://alex-public-doc.s3.amazonaws.com/json_simple-1.1/index.html.
The url connection is to a page that send me a json file.
Any suggest?
Batakj
12.8k17 gold badges68 silver badges114 bronze badges
asked May 31, 2013 at 8:47
Marco Micheli
7173 gold badges9 silver badges26 bronze badges
1 Answer 1
You need to initialize the input stream reader with UTF charset. Here is what you need to update:
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
answered May 31, 2013 at 8:53
Juned Ahsan
68.9k12 gold badges101 silver badges138 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Marco Micheli
I tried this, now i'm getting all "?" chars instead of the \u... chars
Juned Ahsan
Are you printing it on console? If yes then FYI, console wouldnt be able to render your utf-8 output properly
Marco Micheli
I'm not only printing them on console, but also writing them on a txt file and uploading to a mysql database with utf-8 charset properly set.
Pablo Lozano
Are you writing them as UTF-8? FileWriter, for example, takes the OS default encoding, which is NOT UTF-8 if you are on Windows. You have to do the same trick to write as you did to read.
Pablo Lozano
The default character encoding in Java for Mac OS X is MacRoman, see this: en.wikipedia.org/wiki/Mac_OS_Roman#Application_notes
lang-java
new InputStreamReader(urlConnection.getInputStream(), "UTF-8")?-Dfile.encoding="UTF-8"to java.