【发布时间】:2014-10-24 21:01:15
【问题描述】:
我有 Spring 控制器方法,它使用 Google.Gson 返回 json 字符串。问题是,我的 json 字符串包含 UTF-8 内容,当我从浏览器访问 url 时,它会以编码格式显示数据。
@ResponseBody
@RequestMapping(value="/getLocations.json", method = RequestMethod.GET)
public String getLocations(HttpServletRequest request,HttpServletResponse response)throws Exception {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
response.setContentType("application/json; charset=utf-8");
Set<Locations> locs = channelService.getLocations();
String json = gson.toJson(locs);
return json;
}
当我从下面的浏览器地址栏中访问 getLocations.json 时,我得到的是:
[{"id":1,"name":"\u0026#3248;\u0026#3262;\u0026#3255;\u0026#3277;\u0026#3231;\u0026#3277;\u0026#3248;\u0026#3264;\u0026#3247;"},{"id":2,"name":"Districts"}]
知道我要去哪里穿吗?或者我怎样才能将数据解码回来?
【问题讨论】:
-
尝试删除
charset=utf-8 -
@ankur-singhal,还是一样
标签: java json spring-mvc utf-8 gson