jackson objectMapper.readValue 读取并存成想要的类型

两种方式:

java反射里面有个很有趣的东东,反射解析成list很难,故new TypeReference

 

package com.alibaba.fastjson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class TypeReference<T> {

    private final Type type;

    protected TypeReference(){
        Type superClass = getClass().getGenericSuperclass();

        type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
    }

    public Type getType() {
        return type;
    }
    
    public final static Type LIST_STRING = new TypeReference<List<String>>() {}.getType();
}
 
List<MemberVO> members = this.objectMapper.readValue(json, new TypeReference<List<MemberVO>>(){});
 List<Map<String, Object>> rawContatcs = mapper.readValue(appJson, List.class);

你可能感兴趣的:(Jackson)