1

For instance given this

[{ "data" : { "item1": value1, "item2:" value2 }}]

How do you get the values 'value1' and 'value2' when you must first access data?

If the fields were at the root then I could just have the method return a POJO with those field names.

I basically want the below to work.

@GET("/path/to/data/")
Pojo getData();

class Pojo
{
public String item1;
public String item2;
}

IService service = restAdapter.create(IService.class);
Pojo pojo = service.getData();

Log.e("pojo", pojo.item1 + ", " + pojo.item2);

Thank you.

rodly
  • 149
  • 3
  • 14

1 Answers1

0

Try below code to get value1 and value2 using Retrofit and Gson library.

class Pojo
{
    private String item1;
    private String item2;

    //Setters and Getters
}

class Data
{
    private Pojo data;

    //Setters and Getters
}

class MyData
{
    private ArrayList<Data> dataList;

    //Setters and Getters
}

IService service = restAdapter.create(IService.class);
MyData data = service.getData(); 

ArrayList<Data> list = data.getDataList(); // Retrive arraylist from MyData

Data obj = list.get(0); // Get first element from arraylist

Pojo pojo = obj.getData(); // Get pojo from Data 

Log.i("pojo", pojo.item1 + ", " + pojo.item2);
Purushotham
  • 3,770
  • 29
  • 41