The use of ObjectMapper class in jackson

jackson introduction

The ObjectMapper class is the main class of the Jackson library. It provides some functions to convert Java objects to match JSON structure and vice versa. It uses instances of JsonParser and JsonGenerator to implement the actual read/write of JSON.

use

1. Import dependencies
<dependencies>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.5</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>

    </dependencies>
2. Test

2.1 Convert java objects to json objects

	ObjectMapper objectMapper = new ObjectMapper();  

    //序列化的时候序列对象的所有属性  
    objectMapper.setSerializationInclusion(Include.ALWAYS);  

    //反序列化的时候如果多了其他属性,不抛出异常  
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);  

    //如果是空对象的时候,不抛异常  
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);  

    //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式  
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);  
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))  

2.2. Create a pojo class:

@Data
@ToString
@AllArgsConstructor
public class Person {
    
    
    private int id;
    private String name;
    private String password;
}

2.3. ObjectMapperTest class:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengbin.pojo.Person;

public class ObjectMapperTest {
    
    

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

        ObjectMapper objectMapper = new ObjectMapper();
        Person person = new Person(1, "tom", "123");
        String jsonString = objectMapper.writeValueAsString(person); //讲Java对象转成json

        System.out.println("JsonString: " + jsonString);
    }
}

The test results are as follows, talk about converting java objects into json objects
Insert picture description here

3. Convert json object into java object
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengbin.pojo.Person;

import java.io.IOException;

public class ObjectMapperTest {
    
    

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

        ObjectMapper objectMapper = new ObjectMapper();
        Person person = new Person(1, "tom", "123");
        String jsonString = objectMapper.writeValueAsString(person); //讲Java对象转成json

        System.out.println("JsonString: " + jsonString);
        System.out.println("____________________________");

		//讲json对象转成java对象
        Person person1 = objectMapper.readValue(jsonString, Person.class);
        System.out.println(person1.toString());
    }
}

As shown below:
Insert picture description here

4. Java array object and JSON array object conversion
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengbin.pojo.Person;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ObjectMapperTest1 {
    
    

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

        ObjectMapper objectMapper = new ObjectMapper();
        //Java数组转换为JSON数组
        Person person = new Person(1, "tom", "123");
        Person person1 = new Person(2, "jack", "123445");
        List<Person> personList = new ArrayList<>();
        personList.add(person);
        personList.add(person1);
        String jsonString = objectMapper.writeValueAsString(personList);
        System.out.println("JsonString List: " + jsonString);
        System.out.println("_________________________________");

        //Json数组转换为Java数组
        //JavaType
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class);
        List<Person> list = objectMapper.readValue(jsonString,javaType);
        //打印出list中的值
        for (Person person2 : list) {
    
    
            System.out.println(person2.toString());
        }
    }
}

The results are as follows;
Insert picture description here

Guess you like

Origin blog.csdn.net/Anna_Liqi/article/details/115214452