java - 关于json的字符串转换问题
PHP中文网
PHP中文网 2017-04-18 10:17:12
0
2
423

使用反射技术,自己封装一个类,提供一个方法将任意对象转换成一个json格式的字符串

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
迷茫

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 fieldsgettersetter方法,所以你用反射的思路的正确的。
其次可以在项目初始化阶段就把所有的类都使用反射,把所有字段读取并保存下来,可以选择使用类名作为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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!