一、前言
基于Spring MVC的org.springframework.ui.ModelMap数据返回类型,写出JSON字符串后打印显示串带有斜杠\,造成前端H5接口无法正常按照JSON格式显示,效果如下图所示,现在需要将其中的反斜杠\去掉
{\"name\":\"小木人\",\"sex\":\"男\",\"age\":\"30\"}
二、解决方法
1.原来代码方法如下
@ResponseBody @b@ @RequestMapping("/getJSON.do")@b@ public ModelMap getJSON(@Valid TestForm form) { @b@ ModelMap model=new ModelMap();@b@ try {@b@ model=testService.getList();@b@ model.put("responseCode", Constants.SUCCESS_CODE);@b@ model.put("responseMsg", model);@b@ }catch (Exception e) {@b@ model.put("responseCode", Constants.ERR_UNDEFINED_CODE);@b@ model.put("responseMsg", Constants.ERR_UNDEFINED_MSG);@b@ model.put("data", ""); @b@ } @b@ return model;@b@ }
修改结果如下
@ResponseBody @b@ @RequestMapping("/getJSON.do")@b@ public String getJSON(@Valid TestForm form) { @b@ ModelMap model=new ModelMap();@b@ try {@b@ model=testService.getList();@b@ model.put("responseCode", Constants.SUCCESS_CODE);@b@ model.put("responseMsg", model);@b@ }catch (Exception e) {@b@ model.put("responseCode", Constants.ERR_UNDEFINED_CODE);@b@ model.put("responseMsg", Constants.ERR_UNDEFINED_MSG);@b@ model.put("data", ""); @b@ } @b@ return JSON.toJSONString(model);@b@ }
2.结果问题解决了,效果如下
{"name":"小木人","sex":"男","age":"30"}