Java中序列化是我们开发中经常会用用到的方法之一,面试中可能会随口一问,以下简要说明序列化和反序列化的概念和例子。反序列化的问题请参考这里
概念
- 序列化:将java对象转为字节流,可以存储在磁盘或者网络传输,比如存储到redis或网络接口对接。
- 反序列化:将字节流转为java对象,比如网络对接的获取到的字符串转为java对象。
为什么要序列化
需要将java对象保存到redis中或者文本文件中,亦或者需要java对象在网络中传输。
序列化和反序列化的方法
例子
这里使用fastjson包举例说明
java类转json字符串
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
/**
*Java对象转json字符串
*/
public class StringToJSON {
public static void main(String[] args) {
//定义JSON字符串
Employee e = new Employee("易学教程","male", 22);
System.out.println(JSON.toJSONString(e));
Employee e2 = new Employee("易学教程2","male", 23);
Employee e3 = new Employee("易学教程3","male", 25);
List<Employee> list = new ArrayList<>();
list.add(e);
list.add(e2);
list.add(e3);
System.out.println(JSON.toJSONString(list));
}
//JavaBean.java
public static class Employee{
private String name;
private String sex;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
public Employee(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
输出{"age":22,"name":"易学教程","sex":"male"}
[{"age":22,"name":"易学教程","sex":"male"},{"age":23,"name":"易学教程2","sex":"male"},{"age":25,"name":"易学教程3","sex":"male"}]
[{"age":22,"name":"易学教程","sex":"male"},{"age":23,"name":"易学教程2","sex":"male"},{"age":25,"name":"易学教程3","sex":"male"}]
json字符串转Java类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
/**
*解析Json字符串反序列化为Java对象
*/
public class StringToJSON {
public static void main(String[] args) {
//定义JSON字符串
String jsonStr = "{\"age\":22,\"name\":\"易学教程\",\"sex\":\"male\"}";
Employee e = (Employee) JSON.parseObject(jsonStr, Employee.class);//简单java类解析
System.out.println("json字符串转简单java对象:"+e.toString());
//解析带有list的json字符串
String jsonStr2 = "[{\"age\":22,\"name\":\"易学教程\",\"sex\":\"male\"},{\"age\":23,\"name\":\"易学教程2\",\"sex\":\"male\"},{\"age\":25,\"name\":\"易学教程3\",\"sex\":\"male\"}]";
List<Employee> list =
JSONObject.parseObject(jsonStr2, new TypeReference<List<Employee>>(){});
System.out.println(list);
}
//JavaBean.java
public static class Employee{
private String name;
private String sex;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
public Employee() {
}
public Employee(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}
输出json字符串转简单java对象:Employee{name='易学教程', sex='male', age=22}
[Employee{name='易学教程', sex='male', age=22}, Employee{name='易学教程2', sex='male', age=23}, Employee{name='易学教程3', sex='male', age=25}]
[Employee{name='易学教程', sex='male', age=22}, Employee{name='易学教程2', sex='male', age=23}, Employee{name='易学教程3', sex='male', age=25}]