Java 如何在freemarker中转义json字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6383525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to escape json strings in freemarker
提问by Skurpi
We are building a restful api using Spring MVC and freemarker as the templating language. We have chosen to build json responses in the freemarker
我们正在使用 Spring MVC 和 freemarker 作为模板语言构建一个 restful api。我们选择在 freemarker 中构建 json 响应
Example freemarker.ftl:
示例 freemarker.ftl:
{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}
We get a problem when the strings in the values contain quotation marks (or any of the other characters in the JSON syntax).
当值中的字符串包含引号(或 JSON 语法中的任何其他字符)时,我们会遇到问题。
The question: How can I escape these strings using freemarker?
问题:如何使用 freemarker 转义这些字符串?
We have looked at ?xml
or ?html
but they do not cover all relevant characters (such as \
).
我们已查看?xml
或?html
但它们并未涵盖所有相关字符(例如\
)。
EDIT:?js_string
will escape the string to comform with JavaScript. And since JSON is based on JavaScript (JavaScript Object Notation), it will work.
编辑:?js_string
将转义字符串以符合 JavaScript。而且由于 JSON 基于 JavaScript(JavaScript Object Notation),因此它可以工作。
EDIT2:In case a single-quote pops up, ?js_string
will escape it which again leads to invalid JSON. The hotfix for it is:
EDIT2:如果弹出单引号,?js_string
将对其进行转义,这再次导致无效的 JSON。它的修补程序是:
${variable?js_string?replace("\'", "\'")}
and if you really want to be picky:
如果你真的想挑剔:
${variable?js_string?replace("\'", "\'")?replace("\>",">")}
Alternatively if you use Spring: http://www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index-org.springframework.extensions.webscripts.json.jsonutils
或者,如果您使用 Spring:http: //www.springsurf.org/sites/1.0.0.M3/spring-webscripts/spring-webscripts-documentation/reference/html-single/index.html#js-api-index- org.springframework.extensions.webscripts.json.jsonutils
采纳答案by stevevls
You're looking for the ?js_string
operator.
您正在寻找?js_string
运营商。
{
"field1" : "${response.value1?js_string}",
"field2" : "${response.value2?js_string}"
}
That will take care of escaping quotes, backslashes, et. al in order to make your JS happy.
这将处理转义引号、反斜杠等。为了让你的 JS 开心。
Edit: I just saw that they introduced a ?json_string
operator in Freemarker 2.3.19. See herefor exactly how it works. And there was much rejoicing...
编辑:我刚刚看到他们?json_string
在 Freemarker 2.3.19 中引入了一个运算符。请参阅此处了解其工作原理。有很多的欣喜...
回答by Kevin Condon
Use a FreeMarker macroto combine all of the answers above, while making the template more readable and maintainable:
使用FreeMarker 宏组合上述所有答案,同时使模板更具可读性和可维护性:
<#macro json_string string>${string?js_string?replace("\'", "\'")?replace("\>", ">")}</#macro>
{
"field1" : "<@json_string "${response.value1}"/>",
"field2" : "<@json_string "${response.value2}"/>"
}
If you want to reuse the macro in multiple templates, put it in its own file and include the fileinstead of duplicating the macro:
如果要在多个模板中重用宏,请将其放在自己的文件中并包含该文件,而不是复制宏:
<#include "/path/to/macro.ftl">