在 Velocity 中转义大括号 {
抱歉:这是我的错。此错误是由于不正确的 json 生成和 Chrome 扩展“Chrome 中的 JSONView”造成的。查看我自己的答案(我必须自己回答这个问题 - 因为我无法再删除该问题)。
我使用 Velocity(org.apache.velocity 的 Maven 版本 1.7)作为模板引擎,我希望输出如下:
{
total : 234
}
现在当我尝试时:
{
total : $listing.size()
}
我收到错误:
Error: Parse error on line 1:
{ total : 0}
--^
Expecting 'STRING', '}'
当我尝试转义大括号时:
\{
total : $listing.size()
\}
我在最终输出中得到转义字符!:
\{
total : 234
\}
SORRY: This is my bad. This error is due incorrect json produce and to Chrome extension "JSONView in Chrome". See my own answer (I had to answer this myself - as I could not delete the question anymore).
I am using Velocity (Maven version 1.7 of org.apache.velocity) as templating engine, and I want output as follows:
{
total : 234
}
now when I try:
{
total : $listing.size()
}
I get an error:
Error: Parse error on line 1:
{ total : 0}
--^
Expecting 'STRING', '}'
and when I try to escape the curly braces:
\{
total : $listing.size()
\}
I get the escape characters in the final output!:
\{
total : 234
\}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
抱歉,这是由于我生成的 JSON 不正确,Chrome 浏览器的扩展“JSONView”向我指出了这一点。这是因为我的钥匙不是字符串......即我有:
但我应该有:
Sorry this was due incorrect JSON I was producing, which Chrome browser's extension "JSONView" pointed out to me. This was because my keys were not strings... i.e. I had:
but I should've had:
确实有更好的方法。
使用此处接受的答案(如何使用 Apache Velocity 进行 XML 转义?)为了进行初始设置,
您还需要添加速度工具依赖项:
然后您可以使用 Velocity 的转义机制像这样的引擎(适合您的情况)
您还可以在此处查看更多转义选项(http ://velocity.apache.org/tools/devel/generic/EscapeTool.html)
Indeed there is a better way of doing this.
use the accepted answer here (How to XML escaping with Apache Velocity?) to do the initial set-up
you would need to add the velocity-tools dependency as well :
and then you can use the escape mechanism of the Velocity Engine like this (for your case)
you can also checkout more escape options here(http://velocity.apache.org/tools/devel/generic/EscapeTool.html)
我最终创建了一个常量
ocb = {
和ccb = }
并使用$ocb
和$ccb
。我确信有更好的方法。 ;)
I ended up creating a constant
ocb = {
andccb = }
and using$ocb
and$ccb
.I am sure there is a better way. ;)
这看起来像你的情况:
http://velocity.apache.org/engine/devel/user-guide .html#escapinginvalidvtlreferences
更新:
首先尝试执行此操作:
然后将文本设置为:
Here is something that looks like your situation:
http://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
UPDATE:
Try to do this first:
and then make your text this: