关于java:Jackson-里-objectMapperreadValue-如何转换泛型-ListE

40次阅读

共计 528 个字符,预计需要花费 2 分钟才能阅读完成。

Jackson (com.fasterxml.jackson) 的 readValue 办法能够将 json 字符串转换成指定的对象

一般用法

import com.fasterxml.jackson.databind.ObjectMapper;

class Abc {
    Long a;
    
    public Long getA() {return a;}
    public void setA(Long a) {this.a= a;}
}
private ObjectMapper objectMapper;
Abc abc = objectMapper.readValue("{\"a\": 1000396667}", Abc.class);

如果碰见泛型怎么办?比方说 List<E>

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

private ObjectMapper objectMapper;
List<Abc> abc = objectMapper.readValue("[{\"a\": 1000396667}]", new TypeReference<List<Abc>>() {});

正文完
 0