I am facing a problem in returning certain UTF-8 Character Strings which are in my Google Cloud SQL Database. I am sharing my code below.
public JSONArray getObjectsJsonArrayByCountryCodeAndDestination(String countryCode, String destinationCountryStartingLetter)
throws JSONException, UnsupportedEncodingException {
String query = "select * from EMBASSY where COUNTRY_CODE = ? AND DESTINATION LIKE '"+destinationCountryStartingLetter+"%' order by DESTINATION";
jdbcTemplate = new JdbcTemplate(dataSource);
List<Embassy> list = jdbcTemplate.query(query,
new Object[] { countryCode }, new EmbassyMapper());
JSONArray jsonArray = new JSONArray();
if (list.isEmpty()) {
return jsonArray;
} else {
for (Embassy embassy : list) {
String address = new String(embassy.getAddress().getBytes("UTF-8"), "UTF-8");
// Upto here I am getting correct value of Address
JSONObject jsonObject = new JSONObject();
jsonObject.put("ID", embassy.getID());
jsonObject.put("type", embassy.getType());
jsonObject.put("telephone", embassy.getTelephone());
jsonObject.put("address", address);
jsonObject.put("url", embassy.getUrl());
jsonObject.put("destination", embassy.getDestination());
jsonObject.put("status", embassy.getStatus());
jsonObject.put("updatedOn", embassy.getUpdatedOn());
jsonObject.put("countryCode", embassy.getCountryCode());
//System.out.println(jsonObject.toString());
jsonArray.put(jsonObject);
}
return jsonArray;
}
}
I have all correct encoded records saved in my database but when I am returning these in the form of JSONArray to my Ajax Request I am getting certain UTF-8 characters changed. like I got Espaňa as Espa?a why is its so.
I am using json.org library for JSON generation.