Home >Java >javaTutorial >What is the principle of converting json string to java object

What is the principle of converting json string to java object

PHPz
PHPzforward
2023-05-14 14:19:061567browse

1. Gson converts json objects into java objects through reflection

Example:

import com.google.gson.Gson;

public class JsonFormat {

//将json字符串转换为java对象

public void jsonToJava(String json_str, Object obj){

Gson gson = new Gson();

obj = gson.fromJson(json_str, obj.getClass());

}
}

2.Generation of gson

Through new gson(): all default configurations are used

Through JsonBuilder: customize some configuration options, version support, output , customized serialization, deserialization and constructors, etc.

Builder pattern: A type of designer pattern that separates the construction of a complex object from its representation.

The above is the detailed content of What is the principle of converting json string to java object. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete