【问题标题】:Convert json String to List<List<String> with Jackson使用 Jackson 将 json String 转换为 List<List<String>
【发布时间】:2019-02-24 13:51:07
【问题描述】:

我有一个 json 字符串,它是字符串列表的列表,需要将其转换为 List&lt;List&lt;String&gt;&gt; 类的对象。

Here 它说明了如何从字符串转换为列表,但它对我不起作用,因为我无法在 constructCollectionType 中指定 List&lt;List&lt;String&gt;&gt;

这是我的尝试,但不起作用:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<List<String>> list = objectMapper.readValue(response,
   typeFactory.constructCollectionType(List.class, List.class));

如何做到这一点?

【问题讨论】:

  • 您使用哪个版本的Jackson?我测试了它的最新版本,它工作正常。

标签: java json jackson


【解决方案1】:

使用TypeReference&lt;T&gt;:

TypeReference<List<List<String>>> typeReference = 
        new TypeReference<List<List<String>>>() {};
List<List<String>> list = mapper.readValue(json, typeReference);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2021-01-13
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多