简体   繁体   中英

How to parse json data with retrofit for objects within object?

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM