Convert string to JSON in Freemarker

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 02:38:56

问题


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"
}

}

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.

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'

UPDATE: I tried to hard code the json string like

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

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.

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.

Any help guys? Thanks.


回答1:


I think the \n inside the json string could cause some problems. Try replacing it first with (or something similar what will suit your needs)

record.custpage_lineitems?replace("\n", "\\n")

and then do the eval




回答2:


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>

See eval documentation for details.


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

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


来源:https://stackoverflow.com/questions/35431223/convert-string-to-json-in-freemarker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!