2

I'm writing an Android app in which I request and receive a JSON object with keys for values in Japanese. I'm converting the values I want to a string as follows:

String myString = new String(myJSONObject.getString("key").getBytes("UTF8"), "UTF8");

However when I display this it appears as nonsense like:

enter image description here

The keys and values are present and correct.

Why is this?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Ken
  • 30,811
  • 34
  • 116
  • 155
  • 1
    Why are you using the `new String()` constructor at all? You might as well just write `String myString = myJSONObject.getString("key");` Anyway - where does the JSON come from? – Matt Ball Aug 17 '11 at 04:03
  • Right. I tried that first, and then tried the line in the question. – Ken Aug 17 '11 at 04:07
  • The JSON is being read in through an input stream following a http GET. – Ken Aug 17 '11 at 04:27
  • Also, the JSON comes from string entries in a MySQL database held on the server. – Ken Aug 17 '11 at 04:49

3 Answers3

2

Do you get the correct characters when you get the same JSON string with your browser? What is the content encoding of the HTTP request? Are they correct when you output them to the log? You have to figure out if it is a transfer problem, encoding mismatch problem, or just a display problem first. Try answering the questions above to narrow it down.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 1
    Your answer is dead on and complete - it was not only enough to solve my problem but also taught me how to solve these things in future. Thank you. As it happens, the source encoding was Western Latin 1, and `String myString = new String(myJSONObject.getString("key").getBytes("ISO-8859-1"), "UTF8");` is what I need. – Ken Aug 17 '11 at 06:40
  • 1
    Glad it's working. It is however better to set the proper encoding at the server (HTTP response), if possible. If the content encoding is correct, HttpClient (or whatever you are using) would decode the strings correctly, and you wouldn't have to handle this in your code. – Nikolay Elenkov Aug 17 '11 at 06:50
1

If it's not displaying Japanese correctly, then it's because Java is in default font mode, you'll need to update the Java fontconfig.properties file, this needs to be placed in the lib dir of your runtime. I'm not sure if this all applies for Android, as I've not made an app, but it's what I've done for regular Java development.

Also, check this out, it's for German, but UTF-8 still: decode string encoded in utf-8 format in android

Community
  • 1
  • 1
  • Thanks Patrick. It's not this - if I introduce my own Japanese string in the code it displays fine. I did some Googling around this and I learned that there's no `fontconfig` in Android (although there is in other Java builds). – Ken Aug 17 '11 at 04:56
  • I found this http://stackoverflow.com/questions/5318413/android-java-utf-8-json-question it seems to say that it's your server response that provides the JSON isn't in the correct character set. –  Aug 17 '11 at 05:08
  • Thanks. I'm wondering that too. I'm trying to figure this out as much as possible before the server guy gets up. – Ken Aug 17 '11 at 05:51
0

Try "UTF-8" instead of "UTF8"

Mike Sickler
  • 33,662
  • 21
  • 64
  • 90