I am making a simple HTTP GET request with a modification and trying to map the json response to my model. The fact is that json returns an array of multiple Shapes, Shape is an abstract class, so it can be a square, a circle, etc. Each figure has its own specified model, so different fields. How can I map this Shape array to a model?
Web service json response
{
"requestId": 0,
"totalShapes": 2,
"shapes": [
{
"circle": {
"code": 1,
"radius": 220
"color" : "blue"
}
},
{
"square": {
"code": 1,
"size": 220
}
}
]
}
Display of the main results:
public class Result {
@SerializedName("requestId") private int requestId;
@SerializedName("totalShapes") private int totalShapes;
@SerializedName("shapes") private List<Shape> shapes;
}
Abstract class:
public abstract class Shape implements Serializable {
}
A circle:
public class Circle {
@SerializedName("code") private int code;
@SerializedName("radius") private int radius;
@SerializedName("color") private String color;
}
Square:
public class Square {
@SerializedName("code") private int code;
@SerializedName("size") private int size;
}
source
share