I tried a lot of solutions from Stack Overflow but cannot solve the problem.
I have this JSON object with a value, title, in UTF-8 and I need to convert it to a Java String:
{"id":"118","title":"\u00c7\u00c0\u00c7"}
I ended up with this approach but it doesn't work:
String title = new String(JsonObj.getString("title").getBytes(), "US-ASCII");
String title = new String(JsonObj.getString("title").getBytes());
English titles are shown correctly as Wartburg, Wiesmann, Xin Kai. Russian are shown like ÂÀÇ, Âåëòà, ÃÀÇ
What is wrong and how can I convert it into normal characters?
EDIT:
Here is how I am receiving JSON
JSONObject jsonObject = new JSONObject();
try {
// sending empty JSON in this request
String jsonRequest = jsonObject.toString();
Log.v(LOG_TAG, "JSON: " + jsonRequest);
URL url = new URL(STRING_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
// hashing the signature
String md5Signature = MD5Utils.md5Apache(KEY + jsonRequest);
// setting heading property
urlConnection.setRequestProperty(AA_SIGNATURE, md5Signature);
urlConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes(jsonRequest);
wr.flush();
wr.close();
// read the inputshtream into the String
InputStream inputStream = urlConnection.getInputStream();
if (inputStream == null) {
// nothing to do
return null;
}
reader = new BufferedReader(
new InputStreamReader(inputStream));
String inputLine;
StringBuffer buffer = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
buffer.append(inputLine);
}
if (buffer.length() == 0) {
// Stream was empty
return null;
}
// String buffer
String responseJsonStr = buffer.toString();
Log.v(LOG_TAG, "Final String buffer: " + responseJsonStr);
// trying to parse json string and return result
try {
return getCarBrandsOrModelsFromJson(responseJsonStr);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Error closing stream");
}
}
}
return null;
}
Here is how I am parsing
private HashMap<String, Integer> getCarBrandsOrModelsFromJson(String carBrandsOrModelsJsonStr) throws JSONException {
// these are the names of JSON objects needed to be extracted
final String AA_DATA = "data";
final String AA_TITLE = "title";
final String AA_ID = "id";
JSONObject carBrandsJson = new JSONObject(carBrandsOrModelsJsonStr);
JSONArray brandsArray = carBrandsJson.getJSONArray(AA_DATA);
HashMap<String, Integer> carBrandsMap = new HashMap<String, Integer>();
for (int i = 0; i < brandsArray.length(); i++) {
String brand = null;
Integer id;
// Get the JSON object representing the one brand
JSONObject oneBrandJson = brandsArray.getJSONObject(i);
// getting brand and id
// ===================>>> ?
brand = new String(oneBrandJson.getString(AA_TITLE).getBytes(), "UTF8");
// brand = oneBrandJson.getString(AA_TITLE);
brand = oneBrandJson.getString(AA_TITLE);
id = oneBrandJson.getInt(AA_ID);
// adding brand and id into hashmap
carBrandsMap.put(brand, id);
}
// Logging result
for (Map.Entry<String, Integer> entry : carBrandsMap.entrySet()) {
Log.v(LOG_TAG, ("\n" + entry.getKey() + " / " + entry.getValue()));
}
return carBrandsMap;
}
\unnnnis part of the JSON specification (and it is not UTF-8, BTW).\unnnnis part of JSON's syntax. A JSON parser will convert it into whatever encoding it uses. In other words,JsonObj.getString("title")should give you the right string. If it doesn't, then your JSON parser is broken.JsonObj.getString("title"), then how long is the resulting string and which characters does it consist of?"\u00c7\u00c0\u00c7"gives the stringÇÀÇ(\u00c7means U+00C7, LATIN CAPITAL LETTER C WITH CEDILLA, and so on). If that is not what you expect, then it's the JSON encoding that's faulty.