Java将对象序列化成为JSON格式、JSON格式反序列化为Java对象

摘要:Java将对象序列化成为JSON格式、JSON格式反序列化为Java对象。

一:引入jackson的依赖:


  org.codehaus.jackson
  jackson-mapper-asl
  1.9.12


  org.codehaus.jackson
  jackson-core-asl
  1.9.12

二:编写实体对象类Employee

package cn.micai.base.io;

import java.io.Serializable;

/**
 * 描述:
 * 

* * transient使用小结 * 1.一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。 * 2.transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。 * 3.被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。 * * @author: 赵新国 * @date: 2018/6/7 12:10 */ public class Employee implements Serializable { private static final long serialVersionUID = 1L; private int employeeId; private String employeeName; /** * 使用transient关键字,表示该字段不序列化 */ private transient String department; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public String toString() { return "Employee{" + "employeeId=" + employeeId + ", employeeName='" + employeeName + '\'' + ", department='" + department + '\'' + '}'; } }

三:编写使用jackson-all.1.7.6.jar工具包实现将对象序列化为JSON格式、JSON格式反序列化为对象的类

package cn.micai.base.io;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.io.StringWriter;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/8/23 12:03
 * @Description: 使用jackson-all.1.7.6.jar工具包实现将对象序列化为JSON格式、JSON格式反序列化为对象
 */
public class JacksonAllSerializeDeserializeMain {

    /**
     * 将Java对象employee序列化成为JSON格式
     * @param employee
     * @return
     */
    public static String serialize(Employee employee){
        // JSON对象序列化
        String employeeJson = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            StringWriter stringWriter = new StringWriter();
            JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(stringWriter);
            objectMapper.writeValue(jsonGenerator, employee);
            jsonGenerator.close();
            employeeJson = stringWriter.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return employeeJson;
    }

    /**
     * 将JSON格式反序列化为employee对象
     * @param employeeJson
     * @return
     */
    public static Employee deserialize(String employeeJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(employeeJson, Employee.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String [] args) {

        Employee employee = new Employee();
        employee.setEmployeeId(1);
        employee.setEmployeeName("赵新国");
        employee.setDepartment("软件工程师");
        // 序列化
        String serialize = serialize(employee);
        System.out.println(serialize);
        // 反序列化
        Employee deserialize = deserialize(serialize);
        System.out.println(deserialize);

    }

}

四:运行结果:

Java将对象序列化成为JSON格式、JSON格式反序列化为Java对象_第1张图片

你可能感兴趣的:(Java后端技术)