Freemarker:迭代嵌套数组

时间:2015-09-14 23:03:47

标签: arrays hashmap freemarker

我想迭代我在Freemarker中创建的嵌套数组,但无法弄清楚如何打印每个值。

数组var

ALTER FUNCTION [dbo].[DecimalDate](@DateTime datetime)
RETURNS FLOAT
AS
BEGIN
    DECLARE @year INT 
    DECLARE @daysInYear INT
    SET @year = DATEPART(year, @DateTime)
    SET @daysInYear = 337 + DATEPART(DAY, EOMONTH(DATEFROMPARTS(@year, 2, 1)))
    RETURN @year + (DATEPART(DAYOFYEAR, @DateTime) - 1 + (DATEDIFF(second, CONVERT(DATE, @DateTime), @DateTime)/86400.0) )  / @daysInYear
END

迭代数组

<#assign filters = [
    { "p1" : [ "performance" ] },
    { "p2" : [ "nav" ] },
    { "p3" : [ "quarterly" ] },
    { "f1" : [ "50496", "50497", "50501" ]},
    { "f2" : [ "51202", "51196", "51203", "51246", "51207", "51260" ]},
    { "f3" : [ "50504", "50505", "50506" ]},
    { "f4" : [ "50507", "50508", "50509", "50510", "50512" ]}
]>

我想创建一个如下所示的输出:

<#list filters as filter>
    ${filter}'<#sep>,
</#list>

我的错误是:

  

对于“$ {...}”内容:自动预期字符串或其他内容   可转换为字符串(数字,日期或布尔值),   但是这已经评估为extended_hash

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

问题在于Freemarker没有办法将过滤器(哈希)转换为字符串。我想你想要按键功能。你现在的位置

${filter}'<#sep>,

你改为打电话

<#list filter?keys as prop>
    ...
</#list>

并迭代这些键。在您的示例中,每个过滤器只有一个键和值

filter[prop]

将是数组,您还必须迭代。

请参阅Freemarker iterating over hashmap keys

答案 1 :(得分:1)

我认为你使用hash-es列表使数据结构过于复杂。相反,只需使用哈希:

<#assign filters = {
    "p1" : [ "performance" ],
    "p2" : [ "nav" ],
    "p3" : [ "quarterly" ],
    "f1" : [ "50496", "50497", "50501" ],
    "f2" : [ "51202", "51196", "51203", "51246", "51207", "51260" ],
    "f3" : [ "50504", "50505", "50506" ],
    "f4" : [ "50507", "50508", "50509", "50510", "50512" ]
}>

<#list filters?keys as name>
  ${name}: <#list filters[name] as value>'${value}'<#sep> + '|' + </#list><#sep>,
</#list>

(另一种可能性是列表清单,但这不太自然。)