@Component
public class Data{    public Object Get(...)
    {
        Map<String,Object> map = new HashMap<>();
        String result = "<\"password\">";
        map .put("result",result );
    }
}@RestController
public class MyContoler {    @Autowired
    private Data data;    @GetMapping("/test")
    public Object test(...)
    {
        return data.Get(...);
    }
}客户端调用/test后得到的数据是:{"result":"<\"password\">"}
里面多了两个斜杠,
客户端期望的数据是不带斜杠的:{"result":"<"password">"}请问如何让springboot返回json不带斜杠?

解决方案 »

  1.   

    是因为你数据里面带引号了。json必须对这个引号转义才能当成正常的json格式处理。客户端解析后就没问题了。
      

  2.   

    JSON格式的字符串,打印出来的时候Key与Value都会是带“的,如果它的Value里面本身就带有”,显示的时候为保持字符串特性会将Value里面的“前面带上转义符。如果根据Key去获取到这个值,是不带有转义符的。
    见这个:    public static void main(String[] args) {
            Map<String, String> map = new HashMap<>(3);
            map.put("test", "<\"aaa\">");        String jsonStr = JsonMapper.toJsonString(map);
            System.out.println(jsonStr);        map = (Map<String, String>) JsonMapper.fromJsonString(jsonStr, HashMap.class);
            System.out.println(map.get("test"));
        }
    或者:    @GetMapping("/json")
        public Map<String, String> json() {
            Map<String, String> map = new HashMap<>(3);
            map.put("test", "<\"aaa\">");
            return map;
        }
    $(function(){
    $.ajax({
    method: "get",
    url: "http://localhost:8080/test/json",
    success: function(data) {
        console.log(data.test);
    alert(data.test);
    }
    });
    });输出:<"aaa">