0

我在返回 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 一样?为什么会这样。

我正在使用 json.org 库生成 JSON。

4

1 回答 1

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">

检索的道实现

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);
}
于 2013-01-10T19:30:20.590 回答