如何通过对象中的对象的改装来解析json数据? [英] How to parse json data with retrofit for objects within object?

查看:57
本文介绍了如何通过对象中的对象的改装来解析json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上看到了许多用于翻新的JSON示例,但是找不到我拥有的json结构类型.我无法解决它.我有以下json数据,我正尝试使用java在android应用程序中显示该数据:

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.

推荐答案

我认为您需要这样的class:

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;
    }
}

在您编写Datum class时,就像这样:

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;
}

}

您应该拥有这样的API和interface:

And you should have and API interface like this :

@GET("/")
Call<Main> getMain();

这篇关于如何通过对象中的对象的改装来解析json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆