我正在获取存储在数据库中的 JSON(JSON 在数据库中存储为字符串)并将其添加到 Controller 中的模型对象。
@RequestMapping( method = RequestMethod.GET, value = "/all" )
public void getJson(HttpServletRequest httpServletRequest, Model model){
String json = serviceDao.getResponseJson();
System.out.println(json); //Output: {"Response":[{"Id":"1","Name":"GAD"},{"Id":"2","Name":"GBD"}],"Status":"Success"}
model.addAttribute("result",json);
}
但是当我从浏览器调用服务时,转义字符会添加到响应中。
http://localhost:8080/MyApplication/all.json
{"result":"{\"Response\":[{\"Id\":\"1\",\"Name\":\"GAD\"},{\"Id\":\"2\",\"Name\":\"GBD\"}],\"Status\":\"Success\"}"}
你能帮我在不带转义字符的情况下将 JSON 对象发送到 Web 服务中的客户端吗?
最佳答案
而不是将字符串添加到模型中直接返回 JSON
@RequestMapping(value="/all")
public @ResponseBody String getJson(){
//Logic
return json;
}
关于java - 将 JSON 添加到 spring rest Controller 中的模型时如何删除转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117235/