java gson对象转json,使用GSon将匿名Java对象类型转换为JSON

is there any possibility to serialize anonymous object type to JSON using Gson or any else library on JAVA ?

Object objResult = new Object() {

boolean success = true;

String email="mail@gmail.com";

};

String jsonstr = gson.toJson(objResult);

In this example, the string jsonstr returns a null value.

解决方案

Gson doesn't support that feature. From the user guide

Fields corresponding to the outer classes in inner classes, anonymous

classes, and local classes are ignored and not included in

serialization or deserialization

You can use a Class (or other Type) representing the superclass of the anonymous type to describe what to serialize using the overloaded toJson(Object, Type) method, but you'll still lose any new fields defined in the anonymous subclass. (Obviously, with Object, this does nothing, since Object has no fields.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值