[英]Converting Jackson JsonNode array to Java List<String>
I hava a Jackson JsonNode (v2.6.3) which has a json array as one of its fields and I'm looking to convert that array to a java List我有一个 Jackson JsonNode (v2.6.3),它有一个 json 数组作为其字段之一,我希望将该数组转换为 java List
Currently im doing the following problem is line 3:目前我正在做以下问题是第 3 行:
JsonNode jsonNode = getJsonPayload();
JsonNode partial = jsonNode.path("someArrayField");
List<String> z = new ObjectMapper().readValue(partial.traverse(), new TypeReference<ArrayList<String>>(){}); // <- this is the problem area
This feels expensive and improper I would have thought the library would provide a simple call to achieve this.这感觉既昂贵又不恰当,我原以为图书馆会提供一个简单的调用来实现这一点。
What would be the proper/efficient way of obtaining the List?获取列表的正确/有效方法是什么? I've seen a few others follow the same pattern as I did above but the answers are not widely accepted我已经看到其他一些人遵循与我上面相同的模式,但答案并未被广泛接受
Since Jackson 2.11 methods readerForListOf
and readerForArrayOf
are available.由于 Jackson 2.11 方法readerForListOf
和readerForArrayOf
可用。 Thus your code will look like the following:因此,您的代码将如下所示:
JsonNode jsonNode = getJsonPayload();
JsonNode partial = jsonNode.path("someArrayField");
List<String> z = new ObjectMapper().readerForListOf(String.class).readValue(partial);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.