我在返回我的Google Cloud SQL数据库中的某些UTF-8字符串时遇到问题。我在下面分享我的代码。
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;
}
}
我将所有正确的编码记录保存在我的数据库中但是当我以JSONArray的形式将这些记录返回到我的Ajax请求时,我得到了某些UTF-8字符的更改。就像我 Espaňa 一样 Espa?a 为什么会如此。
我正在使用json.org库生成JSON。
答案 0 :(得分:0)
最后,我已经用你们评论的贡献解决了这个问题。我在Spring控制器方法的ResponseBody中添加了这几行,我也在JSP中进行了这些更改:我在我的完整代码下面共享。
JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Dao实施检索
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");
String address2 = embassy.getAddress();
System.out.println(address+" : "+address2);
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;
}
}
弹簧控制器方法
@RequestMapping(value = "/admin/listembassy", method = RequestMethod.GET)
public @ResponseBody void listEmbassyByCountryCode(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap) throws JSONException, JsonGenerationException, JsonMappingException, IOException {
String countryCode = req.getParameter("searchForCountryCode");
String destinationCountryStartLetter = req.getParameter("destinationCountryStartLetter");
EmbassyDao embassyDao = (EmbassyDao) context.getBean("embassyDao");
JSONArray jsonArray = new JSONArray();
if(destinationCountryStartLetter != null && !destinationCountryStartLetter.equals("")){
jsonArray = embassyDao.getObjectsJsonArrayByCountryCodeAndDestination(countryCode, destinationCountryStartLetter);
}else{
jsonArray = embassyDao.getObjectsJsonArrayByCountryCode(countryCode);
}
resp.setContentType("application/json");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().println(jsonArray);
}