如何在 freemarker 中输出 json 文档的整个节点
How can I output an entire node of a json document in freemarker
我正在尝试使用 freemarker 提取 JSON 文档的部分内容并将它们输出到新文档中。我一直 运行 关注 freemarker 抱怨的问题:
SEVERE: Error executing FreeMarker template
FreeMarker template error:
For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to an extended_hash (wrapper: f.c.HashLiteral$SequenceHash):
==> m.name [in template "testjson.ftl" at line 8, column 15]
当元素 m.name 计算为字符串时,我的模板工作正常,但我想用它来提取整个节点。模板是:
<#assign m = doc?eval>
<html>
<head>
<title>Test JSON</title>
</head>
<body>
<h1>${m.name}</h1>
</body>
</html>
结构简单,效果很好:
{
"name" : "joe",
"number": "123-456-7890"
}
但不复杂:
{
"name" :
{
"first" : "joe",
"last" : "developer"
},
"number":"123-456-7890"
}
是否可以轻松提取并输出整个节点?
谢谢,
伊恩
不,目前 (2.3.23) FTL 没有开箱即用的 JSON 支持(除了 JSON 转义)。当然,可以写一个 "toJSON" TemplateMethodModelEx
来做到这一点。
我正在尝试使用 freemarker 提取 JSON 文档的部分内容并将它们输出到新文档中。我一直 运行 关注 freemarker 抱怨的问题:
SEVERE: Error executing FreeMarker template FreeMarker template error: For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to an extended_hash (wrapper: f.c.HashLiteral$SequenceHash): ==> m.name [in template "testjson.ftl" at line 8, column 15]
当元素 m.name 计算为字符串时,我的模板工作正常,但我想用它来提取整个节点。模板是:
<#assign m = doc?eval>
<html>
<head>
<title>Test JSON</title>
</head>
<body>
<h1>${m.name}</h1>
</body>
</html>
结构简单,效果很好:
{
"name" : "joe",
"number": "123-456-7890"
}
但不复杂:
{
"name" :
{
"first" : "joe",
"last" : "developer"
},
"number":"123-456-7890"
}
是否可以轻松提取并输出整个节点?
谢谢,
伊恩
不,目前 (2.3.23) FTL 没有开箱即用的 JSON 支持(除了 JSON 转义)。当然,可以写一个 "toJSON" TemplateMethodModelEx
来做到这一点。