本文介绍了Mustache Java:遍历匿名/无键/顶级数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Question: How to iterate over an array read from json input, that does not have a variable name/key. I did not want to restructure the json file as I'd have to edit the service which generates this json and also other services rely on this file and would have also been affected.

已经使用."发布了针对javascript的解决方案.作为Mustache模板中的数组名称: Mustache可以迭代顶级数组吗?,然后在此处

A solution for javascript has already been posted using "." as array name in the Mustache template: Can mustache iterate a top-level array? and here Iterate over keyless array with mustache?

我对Mustache的Java实现也有同样的疑问.

I had the same question for the java implementation of Mustache.

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

Again, an example of input data (json):

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

推荐答案

使用Jackson将其读取到地图中,然后将其转换回json字符串,这表明Jackson将此数组命名为对象".这是该转换和重新转换的输出:

Reading this with Jackson into a map and then converting it back to a json string showed me that Jackson will name this array "object".Here's an output of that conversion and reconversion:

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

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

So if you use Jackson, we can simply use the identifier "object" in the Mustache template

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

这篇关于Mustache Java:遍历匿名/无键/顶级数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:58