开发者

Desearializing a JSON which comes with an empty field

开发者 https://www.devze.com 2023-02-20 14:15 出处:网络
I\'m trying to deserialize a JSON object like this: public class Example { private String a; private int b;

I'm trying to deserialize a JSON object like this:

public class Example { 
private String a; 
private int b; 
private SubDataExample c; 
//more fields, constructor, getters, etc
} 

public class SubDataExample{ 
private String d; 
private int e; 
//more fields, constructor, getters, etc 
} 

It works well most cases, but开发者_如何学运维 sometimes SubData doesn't bring any data, I get JSON like this:

{"a":"blabla", b:843849, c:""}

Then, GSON fails and I get an exception. Would be possible to fill with a null the "c" field in the deserialized object?

I'm consuming a webservice that I haven't made, so, I have no option about changing the JSON that has to be deserialized.

I would appreciate your help

Thank you very much


I would expect that if there is no SubDataExample, then you should get

{ "a": "blabla", "b": 843849, "c": null }

or

{ "a": "blabla", "b": 843849 }
0

精彩评论

暂无评论...
验证码 换一张
取 消