我希望使用Jackson对象Mapper获得多个Json对象数组的大小。我可以在不使用Jackson树

时间:2018-04-09 23:17:43

标签: java spring jackson

<b>Here is Json:</b>

[{"index":"incident","type":"SmbIncident"},
{"index":"incident","type":"SmbIncident"}
]


IndexPortion is class how can i get both Json object size.

IndexPortion = new ObjectMapper()。readValue(new File(path),    IndexPortion.class);

1 个答案:

答案 0 :(得分:0)

首先,您应该为json对象创建一个实体:

public class MyEntity implements Serializable{
    private String index;
    private String type;
    // getter and setter
}

然后你可以使用JsonUtil from this post

将json数组转换为list
List<MyEntity> entities= JsonUtil.toObject(jsonString,
                            new TypeReference<List<MyEntity>>() {});
int size = entities.size();