json overview-the use of Jackson, the conversion between bean objects (list lists) and json data (serialization and deserialization)

1. json string

  JSON (JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format.

1.1 Features

  1. It is completely independent of the text format of the programming language to store and represent data.

  2. The concise and clear hierarchical structure makes JSON an ideal data exchange language.

  3. It is easy for people to read and write, but also easy for machine to parse and generate, and effectively improve the efficiency of network transmission.
    (The key of the son data must be a string, and the value is not limited)

1.2 json format syntax

  1. JSON data can be directly recognized in JavaScript
  2. In java code, data and json can be converted to each other

2. json conversion tool

  The json conversion tool is to directly convert java objects or collections into strings in json format through some jar packages packaged in java.

2.1 Common conversion tools for json

Insert picture description here

2.2 Use of Jackson

  Because it is a free and open source json conversion tool, and it is also the default json conversion tool of springmvc

2.2.1 Composition of Jaskson

Insert picture description here

  • jackson-annotations: It is Jaskson's annotation package, providing standard annotation function
  • jackson-core: It is the core package and provides related APIs based on " stream mode " analysis, including JsonPaser and JsonGenerator
  • jackson-databind: It is a data binding package that provides related APIs based on " object binding " analysis (ObjectMapper) and "tree model" analysis APIs (JsonNode)

2.2.2 Jackson's ObjectMapper class【***】

  The ObjectMapper class is the main class of the Jackson library, and its main functions are:

  1. Realize the conversion of java objects to json data format (called serialization )
  2. Realize the conversion of json data format to java object (called deserialization )
  • Class can be used ObjectMapper readValue method JSON content deserialize Java objects.
  • Class can be used ObjectMapper writeValue method Java object serialization is JSON data format.

2.3 Case

  1. Import jar packages-Import jar packages in the WEB-INF directory and add them to Library
    Insert picture description here
  2. Create a User object (Java package class attributes are generally private, so get/set methods are required, otherwise Jackson cannot read them)
public class User {
    
    
    private String name;
    private int age;
    private String sex;
    private String address;
	//省略构造方法、getter/setter方法
}

2.3.1 Write a test class, test the writeValueAsString() method of the ObjectMapper class, and convert javabean to json data format

//测试对象序列化,将对象转换为json数据格式
public class TestObjectMapper {
    
    
    //测试单个对象user转换为json数据格式
    @Test
    public void test01() throws JsonProcessingException {
    
    
        //1、创建对象user
        User user = new User("我是GF_浪夏一学 - 单个对象",21,"男","湖南");
        //2、writeValueAsString()方法,将一个user对象转成json字符串
        String userJson = new ObjectMapper().writeValueAsString(user);//将javabean转换为json数据格式
        System.out.println(userJson);
    }

    //将一个对象数组转换为json数据格式
    @Test
    public void test02() throws JsonProcessingException {
    
    
        //1、创建对象,加入集合中
        User user1 = new User("我是集合user1",21,"男","湖南");
        User user2 = new User("我是集合user2",20,"女","北京");

        List<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);
        //2、writeValueAsString()方法,将一个对象数组转换为json数据格式
        String userListJson = new ObjectMapper().writeValueAsString(userList);
        System.out.println(userListJson);
    }

    //将一个Map集合转成json字符串
    @Test
    public void test03() throws JsonProcessingException {
    
    
        //1:创建对象
        User user1 = new User("我是map集合的user1对象",21,"男","湖南");
        User user2 = new User("我是map集合的user2对象",20,"女","北京");
        //存入map
        Map<String,User> map  = new HashMap<>();
        map.put("user1",user1);
        map.put("user2",user2);
        //2、writeValueAsString()方法,将一个Map集合转成json字符串
        String json= new ObjectMapper().writeValueAsString(map);
        System.out.println(json);
    }
}

operation result:

Insert picture description here

2.3.2. Test the readValue method of the ObjectMapper class , and convert the json data format into a javabean object

public class TestObjectMapper {
    
    

    //将一个json数据转换为user对象
    @Test
    public void test04() throws IOException {
    
    
        //1:创建一个json对象
        String jsonData = "{\"name\":\"我是GF_浪夏一学\",\"age\":21,\"sex\":\"男\",\"address\":\"湖南\"}";
        //2、writeValueAsString()方法,将一个Map集合转成json字符串
        User user = new ObjectMapper().readValue(jsonData, User.class);
        System.out.println(user);//会输出user的toString()方法
    }
}

operation result:
Insert picture description here

Convert json data format into list
//测试json数据转集合list
    @Test
    public void test05() throws IOException {
    
    
        String json="[{\"name\":\"我是GF_浪夏一学\",\"age\":21,\"sex\":\"男\",\"address\":\"湖南\"},{\"name\":\"java小皮孩\",\"age\":21,\"sex\":\"男\",\"address\":\"深圳\"}]";

        //
        ObjectMapper objectMapper = new ObjectMapper();

        List<User> userList = objectMapper.readValue(json,new TypeReference<List<User>>(){
    
    });//将json转成对象   参1 json数据  参2 new TypeReference<>(){}
        System.out.println(userList);
    }

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108669100