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>>() {});

益达的库
17 声望0 粉丝

你这个需求做不了