3

I have seen many examples of JSON used for retrofit on the web but unable to find the type of json structure i have. I could not manage to solve it. I have following json data for which i am trying to show in my android app with java:

{
  "main": {
 "data": [
      {
        "Date": "2020-06-15",
        "name": "mary",
        "address": "NY"
      },
      {
        "Date": "2020-06-15",
        "name": "bob",
        "adress": "LA"
      },
      {
        "Date": "2020-06-15",
        "name": "John",
        "address": "CA"
      }
     ]
   }
}

In addition i got following model classes:

-----------------------------------com.example.Datum.java-----------------------------------

package com.example;

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

public class Datum {

@SerializedName("date")
@Expose
private String date;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

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

public class Example {

@SerializedName("main")
@Expose
private Main main;

public Main getMain() {
return main;
}

public void setMain(Main main) {
this.main = main;
}

}
-----------------------------------com.example.Main.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Main {

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

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

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

}

However i am unable to make interface class and use the models in retrofit. Please suggest appropriate way to do it with example. Thanks.

5
  • What error are you getting? Commented Jun 28, 2020 at 18:03
  • Why do you need an interface class here? And I hope you generated this model using jsonschema2pojo Commented Jun 28, 2020 at 18:06
  • @vinv: i am getting empty result. Commented Jun 29, 2020 at 1:40
  • @Sumit Sahoo: to define retrofit calls like "@GET("/") Call<Main> getMain();" Commented Jun 29, 2020 at 1:40
  • Do check @milad answer. It should work. Commented Jun 29, 2020 at 4:09

1 Answer 1

1

I think you need a class like this :

public class Main{

    @SerializedName("main")
    private Main main;

    @SerializedName("data")
    private List<Datum> datas;

    public void setMain(Main main){
        this.main = main;
    }

    public Main getMain(){
        return main;
    }

    public void setCustomers(List<Datum> datas){
        this.datas = datas;
    }

    public List<Datum> getDatas(){
        return datas;
    }
}

And as you write your Datum class something like this :

public class Datum {

@SerializedName("date")
@Expose
private String date;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}

And you should have and API interface like this :

@GET("/")
Call<Main> getMain();
Sign up to request clarification or add additional context in comments.

4 Comments

where does customers come from?
@Bishwash, Change it to data , i updated my answer.
It should be @SerializedName("Date"). Rest all is fine.
@vinv , The response that come from server is date , please check the question again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.