在Freemarker中将字符串转换为JSON [英] Convert string to JSON in Freemarker

查看:2418
本文介绍了在Freemarker中将字符串转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何将VALID JSON STRING转换为freemarker中的实际JSON(序列)的任何方法.我的意思是,这个字符串实际上是通过JSON.stringify()调用返回的.

Any ways on how we can convert VALID JSON STRING to actual JSON(sequence) in freemarker. I mean this string is actually returned by a JSON.stringify() call.

我遵循此帖子所说的内容,但这似乎不适用挖.

I follow what this post says but it seems this is not applicable to mine.

<#assign test = "(record.custpage_lineitems?json_string)">
<#assign m = test?eval>

<#list m as k>
    ${k.item}
</#list>
ERROR says
Expected collection or sequence. m evaluated instead to freemarker.template.SimpleScalar on line 286, column 32 in template.
Sample JSON String
{
"34952": {
    "item": "TRAVEL",
    "notes": "Travel Time To Client Site to Perform Repairs.1.0",
    "type": "Service"
},
"34963": {
    "item": "MECHANIC RECOMMENDATION",
    "notes": "MECHANIC RECOMMENDATION\nr&r drive tires 21x7x15 smooth black \nr&r lp tank latch on bracket \nr&r lp hose cupler",
    "type": "Service"
},
"9938": {
    "item": "T1",
    "notes": "Field Service Call Charge75$ labor 124$",
    "type": "Service"
},
"34549": {
    "item": "GENERAL SERVICE INFO",
    "notes": "SERVICE NOTES:\ndrove to customer location found lift found to broken hydraulic hoses had to remove attachment in order to remove broken hoses then drove to get hoses made installed hoses back on lift re installed loose brackets I found out attachment back on lift topped off hydraulic resivoir and lift was ready",
    "type": "Service"
},
"36264": {
    "item": "FSO PARTS (UN CHECK IF NEEDED)",
    "notes": "MARK CHECK IF PARTS NOT NEEDED.",
    "type": "Service"
},
"36266": {
    "item": "FSO QUOTE (UN CHECK IF NEEDED)",
    "notes": "MARK CHECK IF QUOTE NOT NEEDED.",
    "type": "Service"
},
"29680": {
    "item": "0199992-HY32F",
    "notes": "2 x 0199992-HY32F",
    "type": "Inventory Item"
}

}

似乎它没有转换为有效的序列,因为如果我尝试打印${m},它将显示转义的json字符串.

It seems that it is not converting to a valid sequence because if i'll try to print ${m} it displays the escaped json string.

我正在寻找一种只说<#assign test=toJSON(record.custpage_lineitems)的方式,但是我认为您必须用Java编写方法,因为我是在'netsuite'中这样做的

I am looking for a way that I will just say <#assign test=toJSON(record.custpage_lineitems) but I think you have to write methods in java since I am doing this in 'netsuite'

更新:我尝试对

<#assign m = '{"34952":{"item":"TRAVEL","notes":"Travel Time To Client Site to Perform Repairs.1.0","type":"Service"}....}'>

并尝试遍历,似乎可行.但是,如果我将m的值替换为myvariable,则似乎无法正常工作.我100%确定myvariable既不为null也不为空,但包含相同的JSON字符串.

and try to loop through, it seems working. But if I substitute the value of m to myvariable seems not working. I am 100% sure myvariable is not null nor empty but contains the same JSON string.

我的评估是,如果我可以将myvariable包装到single quote,那么我认为它将解决该问题.我尝试过

My assessment is that, if I could just wrap the myvariable to single quote then I think it will solve the issue. I tried

<#assign m = 'myvariable'> and
<#assign m = '(myvariable)'> and 
<#assign m = '(${myvariable})'> and 
<#assign m = '(myvariable?string)'> etc.

,但没有一个是正确的.有人可以指导我介绍如何将现有变量包装为单引号的正确语法是什么.

but none is correct. Can someone just direct me into what is the proper syntax on how to wrap existing variable to single quote.

有人帮忙吗?谢谢.

推荐答案

如果您的record.custpage_lineitems已经是字符串化的JSON,则不必使用

If your record.custpage_lineitems is already a stringified JSON then you do not have to use ?json_string.

用以下内容替换您的前两行:

Replace your first two lines with this:

<#assign m = record.custpage_lineitems?eval>

有关详细信息,请参见 eval 文档.

See eval documentation for details.

更新: 您的custpage_lineitems是哈希图,并且#list接受序列.试试这个:

Update: Your custpage_lineitems is a hash map and #list accepts sequence. Try this:

<#list m?keys as k>
    ${m[k].item}
</#list>

这篇关于在Freemarker中将字符串转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆