Thymeleaf将JSON字符串作为JSON对象打印到javascript变量中 [英] Thymeleaf print JSON string as JSON object into a javascript variable
问题描述
具体
我需要一种方法来打印 JSON
表示a字符串值通过百科全书进入html页面。
I need a way to print JSON
representation of a string value into the html page via thymeleaf.
详细
我' m具有模型属性
,其中包含一个字符串,该字符串实际上是 JSON的字符串表示
I'm having a model attribute
which contains a string which is actually a string representation of the JSON
我的 thymeleaf
代码
<script th:inline="javascript">
var value = [[${data.scriptValue}]];
</script>
打印变量如下
var value = '[[\"asd\",\"3\"],[\"asd\",\"1\"],[\"asdasd\",\"1\"]]';
但我希望这样的东西是 javascript / JSON
数组
But I want something like this as a javascript/JSON
array
var value = [["asd","3"],["asd","1"],["asdasd","1"]];
如何在百里香中做到这一点?
How to do this in thymeleaf?
注意:我知道我可以从
JSON.Parse
执行此操作,但我需要一种方法从thymeleaf执行此操作:)
Note: I know I can do this from
JSON.Parse
but i need a way to do this from thymeleaf :)
推荐答案
更新 - 2015/12/24
此功能在Thymeleaf 3中可用
This feature is available in Thymeleaf 3
参考 Thymeleaf文本语法
在 https://github.com/thymeleaf/thymeleaf/issues/395
// Unescaped (actual answer)
var value = [(${data.scriptValue})];
//or
var value = [# th:utext="${data.scriptValue}"/];
// Escaped
var value = [[${data.scriptValue}]];
//or
var value = [# th:text="${data.scriptValue}"/];
在Thymeleaf 2不可能正如Patric LC所述,报告了两个问题。
It's not possible at Thymeleaf 2. As Patric LC mentioned, there are two issues reported for this.
使用Jackson进行JSON#81的Javascript内联
这篇关于Thymeleaf将JSON字符串作为JSON对象打印到javascript变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!