使用jackson处理json

package com.example.demospring;


import com.example.demospring.po.User;
import com.example.demospring.po.Wrt;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
class DemospringApplicationTests {
    @Autowired
    ObjectMapper objectMapper;

    /**
     * jackson手动创建jsonObjStr / jsonArrayStr
     */
    @Test
    void creatJson(){
        ArrayNode rootNode = objectMapper.createArrayNode();
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("udsgf",123);
        objectNode.put("gioqw","fieiwq");
        System.out.println(objectNode.toString()); //{"udsgf":123,"gioqw":"fieiwq"}

        rootNode.add(objectNode);
        System.out.println(rootNode.toPrettyString());
        //[ {
        //  "udsgf" : 123,
        //  "gioqw" : "fieiwq"
        //} ]
    }


    


    /**
     * javaObj 转jsonObj str
     * @throws JsonProcessingException
     */
    @Test
    void test1() throws JsonProcessingException {
        User user = new User();
        user.setName("scg");
        user.setId((long) 18);
        user.setUpdateTime(new Date());
        String res = objectMapper.writeValueAsString(user);
        System.out.println(res);  // {"id":18,"name":"scg","updateTime":"2020-06-21T08:59:43.489+0000"}

        //在po.User加上@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")注解后
        System.out.println(res);  // {"id":18,"name":"scg","updateTime":"2020-06-21 09-00-48"}

        //在yml配置jackson 时区和dateFormat后
        System.out.println(res);  // {"id":18,"name":"scg","updateTime":"2020-06-21 17:06:04"}


    }

    /**
     * javaList 转jsonArray str
     * @throws JsonProcessingException
     */
    @Test
    void test2() throws JsonProcessingException {
        User user = new User();
        user.setName("scg");
        user.setId(18L);
        user.setUpdateTime(new Date());
        
        User user2 = new User();
        user2.setName("tcg");
        user2.setId(21L);
        List userList = new ArrayList<>();
        userList.add(user);
        userList.add(user2);
        String res = objectMapper.writeValueAsString(userList);
        System.out.println(res); //[{"id":18,"name":"scg","updateTime":"2020-06-21 17:18:10"},{"id":21,"name":"tcg","updateTime":null}]
        
        
    }


    /**
     * jsonObj str转 javaObj 支持全部/部分解析
     * @throws JsonProcessingException
     */
    @Test
    void test3() throws JsonProcessingException {
        String json = "{\"id\":18,\"name\":\"scg\"}";
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user.toString());
        Wrt w1 = objectMapper.readValue(json, Wrt.class);
        System.out.println(w1.toString());
    }


    /**
     * jsonArray str 转 java List 支持全部/部分解析
     * @throws JsonProcessingException
     */
    @Test
    void test4() throws JsonProcessingException {

        String json = "[{\"id\":18,\"name\":\"scg\"},{\"id\":21,\"name\":\"tcg\"}]";

        // 反序列化,接收两个参数:json数据,反序列化的目标类字节码
        //json转集合比较麻烦,因为你无法同时把集合的class和元素的class同时传递到一个参数。因此Jackson做了一个类型工厂
        List users = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, User.class));
        for (User u : users) {
            System.out.println("u = " + u);
        }

        List wrtList = objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Wrt.class));
        System.out.println(wrtList);


    }
    
    
    
    



}

 

你可能感兴趣的:(JSON)