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()); }
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!