【问题标题】:Mustache Java: Iterating over an anonymous/keyless/top-level arrayMustache Java:遍历匿名/无键/顶级数组
【发布时间】:2019-10-04 11:47:22
【问题描述】:

问题:如何遍历从 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】:

    与 Jackson 一起将其读入地图,然后将其转换回 json 字符串,这表明 Jackson 会将这个数组命名为“对象”。 这是该转换和重新转换的输出:

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

    所以如果你使用 Jackson,我们可以简单地在 Mustache 模板中使用标识符“object”

        {{#object}}
        <tr>
            <td>{{name}}</td>
            <td>{{week}}</td>
            <td>{{date}}</td>
            <td>{{status}}</td>
        </tr>
        {{/object}}
    

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      相关资源
      最近更新 更多