Reputation: 884
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.
Upvotes: 3
Views: 305
Reputation: 1660
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();
Upvotes: 1