Since it is a custom class, it should mean that this class may not necessarily provide all fieldsgetter和setter方法,所以你用反射的思路的正确的。 其次可以在项目初始化阶段就把所有的类都使用反射,把所有字段读取并保存下来,可以选择使用类名作为key,然后自定义一个描述类作为value,放进Map中。 这个描述类描述了类的字段信息。 最后再构造一系列的转化类. These classes are used to convert different types of data. For example, for custom classes, use a custom class converter. When encountering List The class uses List converter, and when it encounters Stirng, it directly outputs the value. In this case, when converting the object into a json string, obtain the description class according to the class name, then traverse the information of the description class, call different conversion classes for different field types to output the fields into strings, and finally splice these strings . The above just provides an implementation idea, welcome to discuss.
What you need is tools like GSON to help you achieve it
Since it is a custom class, it should mean that this class may not necessarily provide all fields
getter
和setter
方法,所以你用反射的思路的正确的。其次可以在项目初始化阶段就把所有的类都使用反射,把所有字段读取并保存下来,可以选择使用
类名作为key
,然后自定义一个描述类作为value
,放进Map
中。这个
描述类
描述了类的字段信息。最后再构造一系列的
转化类
. These classes are used to convert different types of data. For example, for custom classes, use a custom class converter. When encountering List The class uses List converter, and when it encounters Stirng, it directly outputs the value.In this case, when converting the object into a json string, obtain the description class according to the class name, then traverse the information of the description class, call different conversion classes for different field types to output the fields into strings, and finally splice these strings .
The above just provides an implementation idea, welcome to discuss.