Mustache Java:遍历匿名/无键/顶级数组

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

问题:如何遍历从json输入读取的数组,该数组没有变量名/键。我不想重组json文件,因为我不得不编辑生成该json的服务,并且其他服务也依赖于此文件,因此也会受到影响。

已经使用“。”发布了javascript解决方案。作为Mustache模板中的数组名称:Can mustache iterate a top-level array?和此处的Iterate over keyless array with mustache?

我对Mustache的Java实现有相同的问题。

同样,输入数据的示例(json):

[ 
{
  "name" : "test",
  "week" : "first",
  "date" : "Wed Oct 02 14:06:35 GMT 2019",
  "status" : "success"
}
]
java arrays loops mustache anonymous-arrays
1个回答
0
投票

[将Jackson读入地图,然后将其转换回json字符串,这告诉我Jackson将此数组命名为“ object”。这是该转换和重新转换的输出:

{
  "object" : [ {
    "name" : "test",
    "week" : "first",
    "date" : "Wed Oct 02 14:06:35 GMT 2019",
    "status" : "success"
  } ]
}

因此,如果您使用杰克逊,我们只需在Moustache模板中使用标识符“对象”

    {{#object}}
    <tr>
        <td>{{name}}</td>
        <td>{{week}}</td>
        <td>{{date}}</td>
        <td>{{status}}</td>
    </tr>
    {{/object}}
© www.soinside.com 2019 - 2024. All rights reserved.