Create POJO Model with dynamic object value

I have a 3 object value from JSON, and the second object is dynamic value, i.e. it can be an array or an object. So it look like this one :

{
 obj1 : {....},
 dynamicObj2 : {....}, // it can be object or array
 obj3 : {....}
}

So, my question is how can this be achieved in the POJO class? I am using GSON for this case. And it will be implemented at Android end and I am using retrofit for the networking library. Any suggestion for POJO class? Or I must use manual String object and parsing one by one?

Upvotes: 2

Views: 1653

Answers (1)

Raeglan
Raeglan

Reputation: 524

If the Object it can be is of the same kind as the elements in the Array just make it always an array in your Java Class. if not then you will need to use the Object class and cast it to the appropriate type you want later.

You can also automatically generate a POJO using this tool: http://www.jsonschema2pojo.org/ You can even make it serializable, parcelable, and so on. Just check Gson and preview to see if the class is in your liking.

Upvotes: 1

Related Questions