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