I have successfully used GSON to convert a JSON to a single Object and to convert JSON to a list of objects. My problem is that there are 2 sources emitting data to me. One is only sending one object and the other is sending a list of Objects.
Single object from 1st source:
{
id : '1',
title: 'sample title',
....
}
List of objects from 2nd source:
[
{
id : '1',
title: 'sample title',
....
},
{
id : '2',
title: 'sample title',
....
},
...
]
The class being used for deserializing:
public class Post {
private String id;
private String title;
/* getters & setters */
}
Below is working for my 1st case:
Post postData = gson.fromJson(jsonObj.toString(), Post.class);
And this is working for my 2nd case:
Post[] postDatas = gson.fromJson(jsonObj.toString(), Post[].class);
Is there a way to manage both cases? Or should I look into the string and add [] when it is not available Thanks