如何在 freemarker 中转义 json 字符串
我们正在使用 Spring MVC 和 freemarker 作为模板语言构建一个 Restful api。我们选择在 freemarker 示例 freemarker.ftl 中构建 json 响应
:
{
"field1" : "${response.value1}",
"field2" : "${response.value2}"
}
当值中的字符串包含引号(或 JSON 语法中的任何其他字符)时,我们会遇到问题。
问题:如何使用 freemarker 转义这些字符串?
我们已经查看了 ?xml
或 ?html
但它们并未涵盖所有相关字符(例如 \
)。
编辑: ?js_string
将转义字符串以符合 JavaScript。由于 JSON 基于 JavaScript(JavaScript 对象表示法),因此它可以工作。
编辑2:如果弹出单引号,?js_string
将转义它,这再次导致无效的 JSON。它的修补程序是:
${variable?js_string?replace("\\'", "\'")}
如果你真的想挑剔:
${variable?js_string?replace("\\'", "\'")?replace("\\>",">")}
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
Example 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).
The question: How can I escape these strings using freemarker?
We have looked at ?xml
or ?html
but they do not cover all relevant characters (such as \
).
EDIT: ?js_string
will escape the string to comform with JavaScript. And since JSON is based on JavaScript (JavaScript Object Notation), it will work.
EDIT2: In case a single-quote pops up, ?js_string
will escape it which again leads to invalid JSON. The hotfix for it is:
${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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找
?js_string
运算符。这将处理转义引号、反斜杠等。 al为了让你的JS开心。
编辑:我刚刚看到他们在 Freemarker 2.3.19 中引入了
?json_string
运算符。请参阅此处了解其具体工作原理。而且还有很多庆幸的事...You're looking for the
?js_string
operator.That will take care of escaping quotes, backslashes, et. al in order to make your JS happy.
Edit: I just saw that they introduced a
?json_string
operator in Freemarker 2.3.19. See here for exactly how it works. And there was much rejoicing...使用 FreeMarker 宏 组合上述所有答案,同时使模板更具可读性和可维护:
如果您想在多个模板中重用该宏,请将其放入自己的文件中并 包含文件而不是复制宏:
Use a FreeMarker macro to combine all of the answers above, while making the template more readable and maintainable:
If you want to reuse the macro in multiple templates, put it in its own file and include the file instead of duplicating the macro:
从 FreeMarker 2.3.32 开始,它变得更加容易,因为
?c
现在引用字符串:请注意,
${...}
周围没有引号!这就是重点;如果值是字符串(而不是数字或布尔值,它们的格式不添加引号),则?c
将它们放在那里。在 2.3.32 之前,上述内容会因错误而终止,因为
?c
不允许输入字符串,只能输入数字和布尔值。从2.3.32开始,您还可以使用
?cn
代替?c
,它代表“?c nullable”。然后,如果值丢失/null
,它将输出null
,不带引号。因此,您也不必通过有条件地打印引号来使模板复杂化。另外,最好将
c_format
设置设置为“JSON”
或“JavaScript 或 JSON”
。 (或者,如果您将incomplete_improvements
配置设置设置为 2.3.32 或更高版本,则最后一个将是默认值。)Starting from FreeMarker 2.3.32, it's much easier, as
?c
now quotes strings:Note that there are no quotes around the
${...}
! That's the point; the?c
puts them there if the value is a string (as opposed to a number or a boolean, which are formatted without adding quotation marks).Before 2.3.32 the above terminates with error, as string input wasn't allowed by
?c
, only numbers and booleans.Starting from 2.3.32, instead of
?c
, you can also use?cn
, which stands for "?c nullable". Then if the value is missing/null
, it will outputnull
, without the quotes. So you don't have to complicate the template with printing quotes conditionally either.Also, It's a good idea to set the
c_format
setting to"JSON"
, or"JavaScript or JSON"
. (Or, if you set theincompatible_improvements
configuration setting to 2.3.32 or higher anyway, the last will be the default.)