序列化-Json
2021-05-29 11:03
标签:end att tty alt ring 编解码 student 中间 obj 序列化-Json 标签:end att tty alt ring 编解码 student 中间 obj 原文地址:https://www.cnblogs.com/ningbing/p/14743487.html 1 @Data
2 public class SimplePOJO implements Serializable {
3 String field1;
4 String field2;
5 public SimplePOJO(String s1, String s2) {
6 field1 = s1;
7 field2 = s2;
8 }
9 // fastjson逆序列化时要求对象必须要有一个默认的构造函数
10 public SimplePOJO(){}
11 }
12 public static void main(String[] args) throws IOException, ClassNotFoundException {
13 String jsonStr = JSON.toJSONString(student);
14 FileOutputStream fos = new FileOutputStream("C:\\Users\\user\\Desktop\\student.dat");
15 // 将字符串编码为字节数组时要和解码时使用相同字符集格式
16 fos.write(jsonStr.getBytes("utf-8"));
17 fos.close();
18
19 FileInputStream fis = new FileInputStream( "C:\\Users\\user\\Desktop\\student.dat");
20 byte[] bytes = new byte[fis.available()];
21 fis.read(bytes);
22 String deJsonStr = new String(bytes,"utf-8");
23 SimplePOJO deStudent = JSON.parseObject(jsonStr, SimplePOJO.class);
24 }