2

我正在尝试使用 Gson 来解析这个 JSON:

{
    "status": "status",
    "lang": "lang",
    "guid": "guid",
    "name": "name",
    "tags": "tags",
    "address": "address",
    "description": "description",
    "size": "M",
    "url": "http:\/\/",
    "email": "mymail@mysite.com",
    "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
4

2 回答 2

1

你的问题出在课堂openinghours上(顺便说一句,应该是大写的!)。在那里,您尝试将字段解析"schedules"为 a List,正如您在 JSON 中看到的那样,它不是 a List,而是一个对象(它被 包围{ })。

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

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

因此,对您来说最快的解决方案就是将类中的属性类型替换schedulesopeninghours

public schedules schedules;

因为 JSON 字段"schedules"是一个包含多个字段的对象day0day1等等......这正是你的类schedules......所以,这应该适合你!


无论如何,最好的解决方案是在你的课堂上使用Mapthis :openinghours

public Map<String, Day> schedules;

这是最好的选择,因为这正是 JSON 字段所"schedules"代表的......此外,这样你就可以只有一个类Day而不是多个类day0day1等等......这更有意义!

于 2013-08-14T11:38:10.740 回答
0

尝试更换

public class openinghours {
    public List<schedules> schedules;       
    @SerializedName("title")
    public String title;
}

//经过

public class openinghours {
    public  schedules  schedules;       
    @SerializedName("title")
    public String title;
} 

您可以为您的 distance_info() 定义一个类:

class DistanceInfo{
    private int distance;
    private unit;
}

//改变

@SerializedName("distance_info")
public Map<String, String> distance_info = new HashMap<String, String>();

//至

@SerializedName("distance_info")
public DistanceInfo distance_info;
于 2013-08-14T11:41:09.793 回答