ObjectMapper Json字符串的转换处理

package com.example.demo;

import com.example.pojo.User;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 * ObjectMapper Json字符串的转换处理
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class JsonTest {
    @Autowired
    ObjectMapper mapper;
    
    public static class User{
        private String userName2;
        private int age;
        private String password;
        private Date birthday;

        public User() {
        }

        public User(String userName2) {
            this.userName2 = userName2;
        }

        public String getUserName2() {
            return userName2;
        }

        public void setUserName2(String userName2) {
            this.userName2 = userName2;
        }

        public int getAge() {
            return age;
        }

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

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }

    @Test
    public void test() throws Exception{
        //对象转JSON
        User user = new User();
        user.setUserName2("lily");
        user.setAge(26);
        user.setPassword("123456");
        user.setBirthday(new Date());
        System.out.println(user.getClass().getDeclaredClasses());
        String json = mapper.writeValueAsString(user);
        System.out.println("json=" + json);

        //JSON串获取数据
        String json2 = "{\"name\":\"lily\",\"age\":26}";
        JsonNode node = this.mapper.readTree(json2);
        String name = node.get("name").asText();
        int age = node.get("age").asInt();
        System.out.println("姓名=" + name + " ,年龄=" + age);

        //JSON串转对象
        //com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.demo.JsonTest$User: can only instantiate non-static inner class by using default, no-argument constructor
        // at [Source: {"userName":"lily"}; line: 1, column: 2]
        // 方案 userName2跟对象的字段名称保持一致   public static class User{  | 内部类修改为static
        String json3 = "{\"userName2\":\"lily\"}";
        JsonTest obj = new JsonTest();

        //public class User{  |   非静态的方式
//        JsonTest.User user4 =  new JsonTest.User();
//        JsonTest.User user5 =  obj.new User();
//        System.out.println("user4=" + user4);

        //静态
        JsonTest.User user6 = new JsonTest.User();
        System.out.println("user6=" + user6);

        JsonTest.User user2 = mapper.readValue(json3, User.class);
        String name2 = user2.getUserName2();
        int age2 = user2.getAge();
        System.out.println("姓名=" + name2 + " ,年龄=" + age2);

        //JSON串转LIST集合
        String jsonStr = "[{\"userName2\":\"lily\",\"age\":26},{\"userName2\":\"scott\",\"age\":27}]";
        JavaType type = mapper.getTypeFactory().constructParametricType(List.class, User.class);
        List<User> list = mapper.readValue(jsonStr, type);
        String msg = "";
        for (User user3 : list) {
            msg += " " + user3.getUserName2();
        }
        System.out.println("姓名=" + msg);

    }
}

 

posted on 2021-11-25 12:00  oktokeep  阅读(974)  评论(5编辑  收藏  举报