我正在尝试创建一个脚本,该脚本将检查id并导出其名称,但是遇到以下情况;



FTL堆栈跟踪(“〜”表示与嵌套相关):

-失败于:#assign services = {“ externalId”:1 [在第1行第1列的无名模板中]

<#list [services] as st><#if st.externalId == externalId>${st.name}</#if>
<#list st.children as nd<#if nd.externalId == externalId>${nd.name}</#if>
<#list nd.children as rd><#if rd.externalId == externalId>${rd.name}</#if>
<#list rd.children as th><#if th.externalId == externalId>${th.name}</#if>
<#list th.children as ths><#if ths.externalId == externalId>${ths.name}</#if>
<#list ths.children as thb><#if thb.externalId == externalId>${thb.name}</#if>
</#list></#list></#list></#list></#list></#list>`


如果所有externalID都有数字,则脚本可以正常运行,但如果为null,则它们也可以为null。

<#assign services=
{
"externalId": 1,
"name": "one",
"children": [
    {
        "externalId": 2,
        "name": "two",
        "children": [
            {
                "externalId": null,
                "name": "three",
                "children": [
                   {
                             "externalId": 4,
                             "name": "four",
                             "children": [
                                {
                                         "externalId": 5,
                                         "name": "five",
                                         "children": []
                                }         ]
                    }       ]
            }]
     }]
}>


我一直在测试https://try.freemarker.apache.org/

请帮忙!!

最佳答案

在FreeMarker中,您(几乎)总是必须指定一个期望null -s出现的默认值。像:nd.externalId! == external如果nd.externalIdnull视为空字符串。或者,当${th.name!'null'}null时,th.name将打印null

08-18 18:22
查看更多