spring:escapeBody results in invalid JSON

前端 未结 2 682
长情又很酷
长情又很酷 2021-01-21 19:36

I am attempting to escape a string in a JSP to return valid JSON on an AJAX call however the spring:escapeBody tag is not correctly escaping single quotes for JSON. Valid JSON s

2条回答
  •  离开以前
    2021-01-21 20:01

    The Javascript Object Notation specification states that

    Any character may be escaped.

    As such,

    {
        "status": "success",
        "body" : "if you don\'t have \"user\" an account"
    }
    

    is valid JSON.

    If you need to create really custom text, you'll need to generate it yourself in a controller handler method or other component.

    Ideally, you would use a @ResponseBody annotated method with a POJO that represents your status/body JSON object.

提交回复
热议问题