如何在freemarker中转义json字符串

时间:2021-09-10 00:43:57

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_script will escape it which again leads to invalid JSON. The hotfix for it is:

编辑2:如果单引号弹出,?js_script将转义它,这再次导致无效的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

2 个解决方案

#1


23  

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 here for exactly how it works. And there was much rejoicing...

编辑:我刚看到他们在Freemarker 2.3.19中引入了一个?json_string运算符。请看这里确切的工作原理。有很多的欣喜...

#2


2  

Use a FreeMarker macro to 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 file instead of duplicating the macro:

如果要在多个模板中重用宏,请将其放在自己的文件中并包含文件而不是复制宏:

<#include "/path/to/macro.ftl">

#1


23  

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 here for exactly how it works. And there was much rejoicing...

编辑:我刚看到他们在Freemarker 2.3.19中引入了一个?json_string运算符。请看这里确切的工作原理。有很多的欣喜...

#2


2  

Use a FreeMarker macro to 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 file instead of duplicating the macro:

如果要在多个模板中重用宏,请将其放在自己的文件中并包含文件而不是复制宏:

<#include "/path/to/macro.ftl">