Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonReader converting unicode characters wrongly

I am building an Android application and I have JSON data which contains Unicode strings. When receiving the data and using InputStreamReader to parse it, the Unicode strings stay fine. However, when I use JsonReader to parse the JSON, it appears to have converted it into some format. I have looked at the source code and it doesn't seem like it does anything out of the ordinary (obviously I might be missing something) - it just takes the string and saves it into a String variable. Does anyone have any ideas on why my Unicode strings are being converted incorrectly?

Thanks

like image 939
Tereno Avatar asked Jun 17 '26 15:06

Tereno


1 Answers

You can decode your stream to string using InputStreamReader and specify your encoding (e.g. UTF-8 or Unicode), then pass your string to JSONObject or JSONArray to create such object.

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

Reference documents:

  • http://developer.android.com/reference/java/io/InputStreamReader.html
  • http://developer.android.com/reference/android/util/JsonReader.html
  • http://developer.android.com/reference/org/json/JSONObject.html
  • http://developer.android.com/reference/org/json/JSONArray.html
like image 102
Albert Zhong Avatar answered Jun 20 '26 04:06

Albert Zhong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!