java - 将 JSON 添加到 spring rest Controller 中的模型时如何删除转义字符

标签 java json spring rest spring-rest

我正在获取存储在数据库中的 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/

相关文章:

java - 当前选择的 JAXB 库提供程序无效

java - JButton 不发送操作命令

ios - fatal error : Index out of range: file Swift/ContiguousArrayBuffer. swift,第 444 行,有时可以工作

mysql - 当数据库发生变化时如何调用java操作

java - readOnly=true 和 TransactionType Never 有什么区别?

java - 如何在不执行事件的情况下将值从 JSP 传递到 Servlet?

java - 在组合框中选择升序时如何对元素列表进行排序

android - 为什么我们应该使用 XMPP 而不是 JSON 作为简单的(纯文本)消息传递应用程序?

php - 选择,其中 JSON 数组包含

spring - 如何将 Spring Boot 应用程序部署到 Tomcat 6 Servlet 2.5