首页 > 解决方案 > 在 Android 上使用 Retrofit 解析嵌套的 json

问题描述

retrofit在我的android应用程序中使用。在我的 webapi 中,有一个我需要解析的嵌套 json 结果。不是嵌套的很好,但是当我遇到嵌套的时候,我似乎无法解析它们。

这是我的 json 示例。我把它缩短了一点,但都一样。当我执行调用方法时,它只是绕过了 onResponse 或 onFailure。我在想我在我的类文件中犯了一个错误,但我不知道是什么或如何。

JSON:

[
    {
        "name": "All joined for mobile",
        "selectedOnes": [
            "1",
            "2"
        ],
        "lines": [
            {
                "details": "",
                "cancelDetails": null,
                "isCancel": null,
                "hours": [
                    {
                        "arrivalTime": "",
                        "departTime": "07:15:00"
                    },
                    {
                        "arrivalTime": "07:30:00",
                        "departTime": ""
                    }
                ]
            },
            {
                "details": "",
                "cancelDetails": null,
                "isCancel": null,
                "hours": [
                    {
                        "arrivalTime": "",
                        "departTime": "07:30:00"
                    },
                    {
                        "arrivalTime": "07:45:00",
                        "departTime": ""
                    }
                ]
            }
        ]
    }
]

ParsedJsonClass.java:

public class ParsedJsonClass {

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

    @SerializedName("selectedOnes")
    @Expose
    private List<String> selectedOnes = null;

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

    public class lines {

        @SerializedName("details")
        @Expose
        private String details;

        @SerializedName("cancelDetails")
        @Expose
        private Object cancelDetails;

        @SerializedName("isCancel")
        @Expose
        private Object isCancel;

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

        public class hours {

            @SerializedName("arrivalTime")
            @Expose
            private String arrivalTime;

            @SerializedName("departTime")
            @Expose
            private String departTime;
        }
    }
}

标签: javaandroidjsonretrofit2android-json

解决方案


推荐阅读