对象序列化(2):objectMapper.readValue详解

1、反序列化方式

1. 简单的直接Bean.class

2. 复杂的用 TypeReference 

2、反序列化方法

首先说明 readValue 针对String 一共有3个重载,如下:

          public <T> T readValue(String content, Class<T> valueType);简单型,就是 直接  UserBase.class 就可。

          public <T> T readValue(String content, TypeReference<T> valueTypeRef);复杂的可以 用这个

          public <T> T readValue(String content, JavaType valueType);这个书写起来比较麻烦,就不说明了,不常用,前2个已经彻底满足了。

public class UserBase {

    /**
     * 用户名
     */
    private String userName;


    /**
     * 年龄
     */
    private Integer age;


    /**
     * 增加时间
     */
    private Date addTime;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }
}


public class TestMain {

    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        String json1 = "{\"userName\":\"小李飞刀\",\"age\":18,\"addTime\":1591851786568}";
        String json2 = "[{\"userName\":\"小李飞刀\",\"age\":18,\"addTime\":123}, {\"userName\":\"小李飞刀2\",\"age\":182,\"addTime\":1234}]";
        try {
            //1、json字符串转为对象
            UserBase userBase1 = objectMapper.readValue(json1, UserBase.class);
            UserBase userBase2 = objectMapper.readValue(json1, new TypeReference<UserBase>(){});
            System.out.println(userBase1.getUserName());
            System.out.println(userBase2.getUserName());

            //2、json转为map
            Map<String,Object> map = objectMapper.readValue(json1, new TypeReference<Map<String,Object>>(){});
            //map:{userName=小李飞刀, age=18, addTime=1591851786568}
            System.out.println("map:" + map);

            //3、json转为list<bean>
            //使用json1报错,此时需要数组/集合类型: Can not deserialize instance of TEst.UserBase[] out of START_OBJECT token
            List<UserBase> lists = objectMapper.readValue(json2, new TypeReference<List<UserBase>>(){});
            System.out.println(lists.get(0).getUserName());

            //4、json转为数组
            UserBase[] userBases = objectMapper.readValue(json1, new TypeReference<UserBase[]>(){});
            System.out.println(userBases[0].getUserName());


            //序列化
            Map<String,String> map1 = Maps.newHashMap();
            map1.put("name", "小李飞刀");
            map1.put("sex", "男");
            String json = objectMapper.writeValueAsString(map1);
            System.out.println(json);
            //反序列化
            Map<String,String> maps = objectMapper.readValue(json,new TypeReference<Map<String,String>>(){});
            System.out.println(maps.get("name"));
            //反序列化
            List<UserBase> listes = objectMapper.readValue(json,new TypeReference<List<UserBase>>(){});
            System.out.println(listes.get(0).getUserName());
            //反序列化
            UserBase[] userBasess = objectMapper.readValue(json,new TypeReference<UserBase[]>(){});
            System.out.println(userBasess[0].getUserName());

            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            //法一
            List<UserBase> userBases1 = objectMapper.readValue(json2, new TypeReference<List<UserBase>>(){});
            //法二
            JavaType javaType= objectMapper.getTypeFactory().constructCollectionType(List.class, UserBase.class);
            List<UserBase> list2 = objectMapper.readValue(json1, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参考:Jackson objectMapper.readValue 方法 详解 - 记性这么差 - 博客园

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值