0

GoogleCloudSQLデータベースにある特定の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生成にjson.orgライブラリを使用しています。

4

1 に答える 1

0

最後に、私はあなたのコメントの貢献でこの問題を解決しました. スプリング コントローラー メソッドの 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);
}
于 2013-01-10T19:30:20.590 に答える