Home > Java > javaTutorial > How to implement Java deserialization

How to implement Java deserialization

王林
Release: 2023-04-28 10:49:06
forward
1724 people have browsed it

Concept

1. The client obtains the byte stream of the sequence object from the file or network, and reconstructs it through deserialization based on the object status and description information saved in the byte stream. object.

Usage Notes

2. When deserializing, you need to pay attention to the format of the json string. If it is an array, the outermost layer is included with '[ ]'. If it is an object or a Map, it is included with '{ }'. According to the json format that needs to be deserialized, select the method gson.fromJson() that needs to be deserialized. Which parameter should be passed and deserialized into an object? The parameter should be the reflection of this object

Instance

public static void main(String[] args) {
      // 定义json
      String json = "{\"name\":\"转换对象\",\"sex\":\"女\",\"age\":20}";
      //实例化Gson
      Gson gson = new Gson();
      //把json序列化为实体类(Test)对象
      Test test = gson.fromJson(json, Test.class);
      //输出---反序列化后的结果为:StudentEntity{name='转换对象', sex='女', age=20}
      System.out.println("反序列化成实体类后的结果为:"+test.toString());
  }
Copy after login

The above is the detailed content of How to implement Java deserialization. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template