I'm familiar with parsing basic JSON data with Retrofit, but struggling with implementation of correct POJO objects for this JSON responce.
Here is JSON data:
{
"observations": [
{
"id": "0",
"type": "1st type",
"data": [
{
"name": "some_name",
"result": "some_result"
}
]
},
{
"id": "1",
"type": "2nd type",
"data": [
{
"name2": "some_name2",
"measurement": "some_measurement",
"field": "some_field",
"result2": "some_result2"
}
]
}
]
}
I have created Classes for both Observation types:
public class DataType1{
@SerializedName("name")
@Expose
private String name;
@SerializedName("measurement")
@Expose
private String measurement;
@SerializedName("field")
@Expose
private String field;
@SerializedName("result")
@Expose
private String result;
}
public class DataType2 {
@SerializedName("name")
@Expose
private String name;
@SerializedName("result")
@Expose
private String result;
}
The general idea what by "type" I determine the type of data and send it to corresponding class:
if(response.body().getType.equals("1st type"))
{
Gson gson = new GsonBuilder().create();
DataType1 data = gson.fromJson(response.body().getObservation, DataType1.class);
}
I assume, next I have to create a separate ObservationsResponce class to get a List of observations:
public class Observation {
@SerializedName("id")
@Expose
private String id;
@SerializedName("type")
@Expose
private String type;
@SerializedName("observation")
@Expose
private List<??What Should Be Here??> observation = null;
}
But the problem is that observations can have a different data types and a so different fields inside. In that case which class should this List be?