我正在尝试使用Gson解析此JSON:

{
    "status": "status",
    "lang": "lang",
    "guid": "guid",
    "name": "name",
    "tags": "tags",
    "address": "address",
    "description": "description",
    "size": "M",
    "url": "http:\/\/",
    "email": "[email protected]",
    "fax": "",
    "tel": "000 000 00 00",
    "total_votes": "0",
    "total_value": "0",
    "rate": 5,
    "open2424": "0",
    "category_main_name": "category_main_name",
    "category_name": "category_name",
    "category_main_name2": "category_main_name2",
    "category_name2": "category_name2",
    "category_main_name3": "category_main_name3",
    "category_name3": "category_name3",
    "park_type": "park_type",
    "park_handicap": "0",
    "park_free": "1",
    "park_description": "",
    "datemodinfo": "2012-12-15 18:18:05",
    "sponsor": "2",
    "sponsorstart": "2012-12-16 13:38:51",
    "sponsorend": "2013-12-16 13:38:51",
    "zip": "zip",
    "town": "town",
    "area": "area",
    "latitude": "latitude",
    "longitude": "longitude",
    "distance_info": {
        "distance": 10,
        "unit": "unit"
    },
    "image": "image",
    "url": "url",
    "open": "1",
    "openinghours": [{
        "schedules": {
            "day0": {
                "periods": [{
                    "from": "09:00",
                    "to": "12:30"
                },
                {
                    "from": "14:00",
                    "to": "18:00"
                }],
                "date": "2013-08-12"
            },
            "day1": {
                "periods": [{
                    "from": "09:00",
                    "to": "12:30"
                },
                {
                    "from": "14:00",
                    "to": "18:00"
                }],
                "date": "2013-08-13"
            },
            "day2": {
                "periods": [{
                    "from": "09:00",
                    "to": "12:30"
                },
                {
                    "from": "14:00",
                    "to": "18:00"
                }],
                "date": "2013-08-14"
            },
            "day3": {
                "periods": [{
                    "from": "09:00",
                    "to": "12:30"
                },
                {
                    "from": "14:00",
                    "to": "18:00"
                }],
                "date": "2013-08-15"
            },
            "day4": {
                "periods": [{
                    "from": "09:00",
                    "to": "12:30"
                },
                {
                    "from": "14:00",
                    "to": "18:00"
                }],
                "date": "2013-08-16"
            },
            "day5": {
                "date": "2013-08-17"
            },
            "day6": {
                "date": "2013-08-18"
            }
        },
        "title": "title"
    }]
}


我用以下代码解析JSON:

Gson gson = new Gson();
new GsonBuilder().serializeNulls().create();
Reader reader = new InputStreamReader(source);
response = gson.fromJson(reader, ResponseShow.class);


这是我的ResponseShow类:

public class ResponseShow {

    @SerializedName("status")
    public String status;
    @SerializedName("lang")
    public String lang;
    @SerializedName("guid")
    public String guid;
    @SerializedName("name")
    public String name;
    @SerializedName("tags")
    public String tags;
    @SerializedName("address")
    public String address;
    @SerializedName("description")
    public String description;
    @SerializedName("size")
    public String size;
    @SerializedName("url")
    public String url;
    @SerializedName("email")
    public String email;
    @SerializedName("fax")
    public String fax;
    @SerializedName("tel")
    public String tel;
    @SerializedName("total_votes")
    public String total_votes;
    @SerializedName("total_values")
    public String total_values;
    @SerializedName("rate")
    public String rate;
    @SerializedName("open2424")
    public String open2424;
    @SerializedName("category_main_name")
    public String category_main_name;
    @SerializedName("category_name")
    public String category_name;
    @SerializedName("category_main_name2")
    public String category_main_name2;
    @SerializedName("category_name2")
    public String category_name2;
    @SerializedName("category_main_name3")
    public String category_main_name3;
    @SerializedName("category_name3")
    public String category_name3;
    @SerializedName("park_type")
    public String park_type;
    @SerializedName("park_handicap")
    public String park_handicap;
    @SerializedName("park_free")
    public String park_free;
    @SerializedName("park_description")
    public String park_description;
    @SerializedName("datemodinfo")
    public String datemodinfo;
    @SerializedName("sponsor")
    public String sponsor;
    @SerializedName("sponsorstart")
    public String sponsorstart;
    @SerializedName("sponsorend")
    public String sponsorend;
    @SerializedName("town")
    public String town;
    @SerializedName("area")
    public String area;
    @SerializedName("latitude")
    public String latitude;
    @SerializedName("longitude")
    public String longitude;
    @SerializedName("distance_info")
    public Map<String, String> distance_info = new HashMap<String, String>();
    @SerializedName("zip")
    public String zip;
    @SerializedName("image")
    public String image;
    @SerializedName("ligoo_url")
    public String ligoo_url;
    @SerializedName("open")
    public int open;
    public List<openinghours> openinghours;
    @SerializedName("query")
    public String query;
}


这是我的openinghours类:

public class openinghours {

    public List<schedules> schedules;
    @SerializedName("title")
    public String title;

}


这是我的Schedules类:

public class schedules {

    public List<day0> day0;
    public List<day1> day1;
    public List<day2> day2;
    public List<day3> day3;
    public List<day4> day4;
    public List<day5> day5;
    public List<day6> day6;
}


还有我的day0类:

public class day0 {

    @SerializedName("date")
    public String date;
    public List<periods> periods;
}


问题是尝试解析day0时出现以下错误:

Error: java.lang.IllegalStateException: Expected BEGIN_ARRAY goal was BEGIN_OBJECT at line 1 column 2414

最佳答案

您的问题出在类openinghours中(顺便说一句,它应该是大写的!)。在这里,您尝试将字段"schedules"解析为List,并且您可以在JSON中看到,它不是List,而是对象(被{ }包围)。

具体来说,"schedules"字段如下所示:

"schedules": {
    "day0": {
        ...
    },
    "day1": {
        ...
    },
    ...
}


因此,最快的解决方案是将schedules类中属性openinghours的类型替换为:

public schedules schedules;


因为JSON字段"schedules"是一个包含几个字段day0day1等的对象,而这恰恰是您的类schedules的含义,所以这应该对您有用!



无论如何,最好的解决方案是在您的Map类中使用openinghours这个:

public Map<String, Day> schedules;


这是最好的选择,因为这正是JSON字段"schedules"所代表的含义...而且,通过这种方式,您只能拥有一个类Day,而不是许多类day0day1等。更有意义!

10-02 04:40