Freemarker:将日期从科学计数法转换为数字

问题描述 投票:0回答:1

我是一个完全的Freemarker新手,我正在使用使用freemarker模板的框架。我正在尝试根据某个字段“日期”对哈希序列进行排序。

我的输入json如下所示:

{"fields": [
                [
                    {
                        "contentType": "application/json",
                        "date": 1.563457325E9,
                        "id": "abc",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426843E9,
                        "id": "def",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563454092E9,
                        "id": "ghi",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563425862E9,
                        "id": "jkl",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426128E9,
                        "id": "mno",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563453696E9,
                        "id": "pqr",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426813E9,
                        "id": "stu",
                        "size": 0.0
                    },
                    {
                        "contentType": "application/json",
                        "date": 1.563426177E9,
                        "id": "vwx",
                        "size": 0.0
                    }
                ]
            ]
        }

[当我尝试执行此操作时:<#assign j=fields[0].eval>时,出现以下错误:

未能通过“?eval”字符串出现此错误:--- begin-message ---语法第1行第55列的?eval-ed字符串错误:遇到“ E9”,但是期望以下之一:“ ..”“ .. ”“,”“}”“。” “ [”“(”“?” “!” “?” “ +”“-”“”“ /”“%”“!=”“ =”“ =”“> =”“>”--- end-message ---失败的表达式:==>fields [0]?eval [在模板“ 89-1070010335”的第1行第14列中)----FTL堆栈跟踪(“〜”表示与嵌套相关)

我想做这样的事情:

<#assign j=fields[0]>
<#list j?sort_by("date") as i>
  ${i.date}: ${i.id}
</#list>

如何将日期符号从科学计数法转换为freemarker中的数字,然后根据此日期字段的值进行sort_by?

如果我能从Freemarker documentation note中获得一些指针或任何特定的引用,我会很高兴。

freemarker
1个回答
0
投票

由于数据模型包含嵌套列表,所以我们也必须在模板中采用嵌套列表指令,如下所示。

  <#list fields as field>
    <#list field?sort_by("date") as innerField>
      ${innerField.date?replace(",", "")} : ${innerField.id}
    </#list>
    </#list>

下面的代码段将科学计数转换为数字。

<#assign scientificFormat = "1.563426177E9">
<#assign number = scientificFormat?number?replace(",", "")>
${number}

来源:https://freemarker.apache.org/docs/ref_builtins_sequence.html


0
投票
<#list fields as field>
<#list field?sort_by("date") as innerField>
  ${innerField.date?replace(",", "")} : ${innerField.id}
</#list>
</#list>
© www.soinside.com 2019 - 2024. All rights reserved.