【问题标题】:GSON, doesn't parse info for objects inside a list of another list of objectsGSON,不解析另一个对象列表中的对象的信息
【发布时间】:2018-10-24 17:09:03
【问题描述】:

我在序列化 JSON 时遇到问题,希望您能理解我的问题,因为我自己解释时遇到了一些问题。

我正在尝试使用 GSON 序列化 JSON 对象。一切正常,除非我在另一个对象数组中有一个对象数组。

我有一个名为 Garden 的类,其中包含 Season 类对象的 ArrayList,其中包含 Schedule 类对象的 ArrayList。

GSON 可以很好地解析 Garden 和 Season 类,但是当涉及到 Schedule 类(Season 类中的那个)时,它会检测到有要添加到其中的项目,因为它添加了对象,即 Season 中的 Schedules 的 ArrayList class 不是空的,但是 class 的属性没有接收到任何内容,我做错了什么?

我的花园课:

public class Garden implements Serializable {
    private String id;
    private float distance;
    @SerializedName("foto_principal")
    private String mainImageURL;
    ...
    @SerializedName("epocas")
    private ArrayList<Season> seasons;

我的季节班:

public class Season implements Serializable {
    @SerializedName("nome")
    private String name;
    ...
    @SerializedName("horarios")
    private ArrayList<Schedule> schedules;

我的日程安排课程:

public class Schedule implements Serializable {
    @SerializedName("nome")
    private String name;
    ...
    @SerializedName("epoca") // todo delete after
    private int  epoca;

我的 JSON 的一部分:

{
meta: {
limit: 1000,
next: null,
offset: 0,
previous: null,
total_count: 11
},
objects: [  <- Here each object is a Garden
{
dificuldade: "Média",
duracao_visita: 60,
epocas: [  <- ArrayList of Seasons
{
    dia_fim: 31,
    dia_inicio: 1,
    horarios: [   <- ArrayList of Schedules
        {
            dias: [
                "0",
                "1",
                "2",
                "3",
                "4"
            ],
            epoca: 1,
            hora_de_abertura: "09:00:00",
            hora_de_fecho: "09:00:00",
            nome: "aa"
        }
    ],
    id: 1,
    mes_fim: 12,
    mes_inicio: 1,
    nome: "Todo o Ano"
},
{
    dia_fim: 21,
    dia_inicio: 8,
    horarios: [
        {
            dias: [
                "5",
                "6"
            ],
            epoca: 2,
            hora_de_abertura: "09:00:00",
            hora_de_fecho: "09:00:00",
            nome: "b"
        }
    ],
    id: 2,
    mes_fim: 9,
    mes_inicio: 6,
    nome: "Verao"
}
],
especies: [ ],
latitude: 37.775318,
longitude: -25.314724,

}

GSON 代码:

public static ArrayList<?> parseJSON(String json, Class obj) {
    ArrayList<Object> dataList = new ArrayList<>();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();

    if (json != null) {
        JsonParser parser = new JsonParser();
        JsonObject root = parser.parse(json).getAsJsonObject();
        JsonArray elements = root.getAsJsonArray("objects");

        for (JsonElement e : elements) {
            JsonObject element = e.getAsJsonObject();
            dataList.add(gson.fromJson(element.toString(), obj));
        }
    }
    return dataList;
}

【问题讨论】:

    标签: java android serialization gson


    【解决方案1】:

    这个问题自己解决了,第二天又回来工作,它工作正常。不知道它可能是什么。我没有更改代码或更改任何来自 JSON 的数据,哦,好吧!

    编辑:经过进一步调查,我得出结论,问题在于服务器以不寻常的方式发送信息,而不是应用程序的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多