您的位置:首页 > 编程语言 > Java开发

SpringMVC Controller后台返回Json到前台乱码问题

2018-03-28 10:07 453 查看
乱码问题主要是字符编码的问题
可通过指定字符格式来解决,不多说,直接上图
@RequestMapping(value = { "json" } ,produces = "application/json; charset=utf-8")
@ResponseBody
public String json(Operation operation,Model model,HttpServletRequest request,HttpServletResponse response) {
JSONObject outobj=new JSONObject();  
  JSONArray contentManagement =new JSONArray();  
  JSONObject innerobj = new JSONObject();
  innerobj.put("title", "文章列表");
  innerobj.put("icon", "icon-text");
  innerobj.put("href", "page/news/newsList.html");
  innerobj.put("spread", false);
  contentManagement.put(innerobj);
  outobj.put("contentManagement", contentManagement);  
return outobj.toString();

}
前台显示不是json数据格式的话有两种解决办法
1、后台直接指定返回数据的格式为json,如上代码
2、前台使用Json.parse(data)来进行格式转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json springmvc