0

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?

Alex
  • 350
  • 5
  • 20

2 Answers2

1
MyObservation.java


public class MyObservation
{
    private Observations[] observations;

    public Observations[] getObservations ()
    {
        return observations;
    }

    public void setObservations (Observations[] observations)
    {
        this.observations = observations;
    }

    @Override
    public String toString()
    {
        return "MyObservation [observations = "+observations+"]";
    }
}


Observations.java

public class Observations
{
    private Data[] data;

    private String id;

    private String type;

    public Data[] getData ()
    {
        return data;
    }

    public void setData (Data[] data)
    {
        this.data = data;
    }

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [data = "+data+", id = "+id+", type = "+type+"]";
    }
}

Data.java

public class Data
{
    private String result;

    private String name;


    //add these fields manually
    private String measurement;

    private String field;

    public String getResult ()
    {
        return result;
    }

    public void setResult (String result)
    {
        this.result = result;
    }


    public String getMeasurement ()
    {
        return measurement;
    }

    public void setMeasurement (String measurement)
    {
        this.measurement = measurement;
    }

    public String getField ()
    {
        return field;
    }

    public void setField (String field)
    {
        this.field = field;
    }


    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [result = "+result+", name = "+name+"]";
    }
}

And its better to use same key and then also if you want different keys and want to generate POJO then use this link

primo
  • 1,340
  • 3
  • 12
  • 40
0

Check this pojo class I am not sure it's work or not

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Datum {

@SerializedName("name")
@Expose
private String name;
@SerializedName("result")
@Expose
private String result;
@SerializedName("name2")
@Expose
private String name2;
@SerializedName("measurement")
@Expose
private String measurement;
@SerializedName("field")
@Expose
private String field;
@SerializedName("result2")
@Expose
private String result2;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getResult() {
    return result;
}

public void setResult(String result) {
    this.result = result;
}

public String getName2() {
    return name2;
}

public void setName2(String name2) {
    this.name2 = name2;
}

public String getMeasurement() {
    return measurement;
}

public void setMeasurement(String measurement) {
    this.measurement = measurement;
}

public String getField() {
    return field;
}

public void setField(String field) {
    this.field = field;
}

public String getResult2() {
    return result2;
}

public void setResult2(String result2) {
    this.result2 = result2;
}

}

class Example {

@SerializedName("observations")
@Expose
private List<Observation> observations = null;

public List<Observation> getObservations() {
    return observations;
}

public void setObservations(List<Observation> observations) {
    this.observations = observations;
}

}

class Observation {

@SerializedName("id")
@Expose
private String id;
@SerializedName("type")
@Expose
private String type;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

}
Shudipto Trafder
  • 1,932
  • 1
  • 14
  • 27