Jackson的用法

Jackson的用法

Jackson对对象、List、Map、数组等详细使用方法

public class JacksonUtil {

    public static void main(String[] args) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 不保留null

        Student student = new Student();

        student.setId(1);
        student.setName("小米");
        student.setAge(6);

        System.out.println("-----------对象转JSON字符串-------------");
        /*
         * 对象转JSON字符串
         */
        System.out.println(objectMapper.writeValueAsString(student));


        System.out.println("-----------Map转JSON字符串-------------");
        /*
         * Map转JSON字符串
         */
        Map<String, Object> map = new HashMap<>();
        map.put("MI", student);
        System.out.println(objectMapper.writeValueAsString(map));

        System.out.println("-----------数组转JSON字符串-------------");
        /*
         * 数组转JSON字符串
         */
        String[] arrays = {"小米", "华为", "三星"};
        System.out.println(objectMapper.writeValueAsString(arrays));

        System.out.println("-----------JSON字符串转对象--------------");
        /*
         * JSON字符串转对象
         */
        String s1 = "{\"id\":1,\"name\":\"小米\",\"age\":6}";
        System.out.println(objectMapper.readValue(s1, Student.class));

        System.out.println("-----------JSON字符串转Map-------------");
        /*
         * JSON字符串转Map
         */
        String s2 = "{\"MI\":{\"id\":1,\"name\":\"小米\",\"age\":6}}";
        System.out.println(objectMapper.readValue(s2, Map.class));

        System.out.println("-----------JSON字符串转对象数组List<Object>------------");
        /*
         * JSON字符串转对象数组List<Object>
         */
        String s3 = "[{\"id\":1,\"name\":\"小米\",\"age\":6}, {\"id\":2,\"name\":\"华为\",\"age\":6}]";
        CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Student.class);
        List<Student> studentList = objectMapper.readValue(s3, listType);
        System.out.println(studentList);

        System.out.println("-----------JSON字符串转Map数组List<Map<String, Object>>------------");
        /*
         * JSON字符串转Map数组List<Map<String, Object>>
         */
        String s4 = "[{\"MI\":{\"id\":1,\"name\":\"小米\",\"age\":6,\"grade\":null,\"ext\":null,\"balance\":null}}, {\"HW\":{\"id\":2,\"name\":\"华为\",\"age\":6,\"grade\":null,\"ext\":null,\"balance\":null}}]";
        CollectionType listType2 = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, map.getClass());
        List<Map<String, Object>> studentList2 = objectMapper.readValue(s4, listType2);
        System.out.println(studentList2);

        System.out.println("-----------JACKSON默认将对象转换为LinkedHashMap------------");

        String expected = "[{\"name\":\"Logan\"},{\"name\":\"IronMan\"},{\"name\":\"SpiderMan\"}]";
        ArrayList arrayList = objectMapper.readValue(expected, ArrayList.class);
        System.out.println(arrayList);


        System.out.println("-----------bean转换为map----------------------------------");
        Student student2 = new Student();
        student2.setId(2);
        student2.setName("华为");
        student2.setAge(11);

        List<Student> students = new ArrayList<>();
        students.add(student);
        students.add(student2);

        List<Map<String, Object>> xxxList = objectMapper.convertValue(students, new TypeReference<List<Map<String, Object>>>() {});

        System.out.println(xxxList);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值