Java 使用GSON错误解析JSON
我从服务器得到了这个Json响应Java 使用GSON错误解析JSON,java,json,gson,Java,Json,Gson,我从服务器得到了这个Json响应 [ { "genre": [ "2", "17" ], "terms": { "ID Criteria": [ "Photo ID", "Over 21s" ], "Dress Code criteria": [ "" ] }, "images": [
[
{
"genre": [
"2",
"17"
],
"terms": {
"ID Criteria": [
"Photo ID",
"Over 21s"
],
"Dress Code criteria": [
""
]
},
"images": [
"http://www.cool.eu/fixr/images/events/d_1000/size800_lasers-2520cd46e8107e.jpg",
"http://www.cool.eu/fixr/images/events/d_1000/size800_fabric-4520cd48414f19.jpg"
],
"codes": [
{
"code": "HC0989",
"price": "8.00"
}
],
"id": "1",
"live": "1",
"venueId": "1",
"title": "Eden Project",
"subTitle": "",
"date": "2013-08-24",
"openTime": "Saturday 24th August",
"closeTime": "10:30 - 05:30",
"price": "1.00",
"bookingFee": "1.50",
"standby": "0",
"djs": "Tim Cullen, Santero, Adam K, Matt May, R3wire, Dj Ez, Shame Khoe",
"presenter": "",
"overview": "As with any top DJ these days, it’s hard to pigeon-hole someone into one specific sound/genre, but Tim’s sets would perhaps be best described as ‘chunky tech-house grooves, spliced with main room progressive house synths’. This warm, uplifting and energet",
"notes": "",
"cutOff": "",
"firstCutOff": "2013-08-24 11:30:00",
"secondCutOff": "2013-08-24 00:00:00",
"autoCutOff": "2013-08-25 02:30:00",
"tickets": "20",
"payment": "0"
},
{
"genre": [
"2",
"17"
],
"terms": {
"ID Criteria": [
"Photo ID",
"Over 21s"
],
"Dress Code criteria": [
""
]
},
"images": [
"http://www.cool.eu/fixr/images/events/d_1000/size800_fabric-2520cd771cfd88.jpg"
],
"codes": [
{
"code": "HC0989",
"price": "8.00"
}
],
"id": "2",
"live": "1",
"venueId": "1",
"title": "Eden Project",
"subTitle": "",
"date": "2013-10-19",
"openTime": "Thursday 19th September",
"closeTime": "10:30 - 05:30",
"price": "10.00",
"bookingFee": "1.50",
"standby": "0",
"djs": "Tim Cullen, Santero, Adam K, Matt May, R3wire, Dj Ez, Shame Khoe",
"presenter": "",
"overview": "As with any top DJ these days, it’s hard to pigeon-hole someone into one specific sound/genre, but Tim’s sets would perhaps be best described as ‘chunky tech-house grooves, spliced with main room progressive house synths’. This warm, uplifting and energet",
"notes": "",
"cutOff": "",
"firstCutOff": "2013-09-19 11:30:00",
"secondCutOff": "2013-09-19 00:00:00",
"autoCutOff": "2013-09-20 02:30:00",
"tickets": "20",
"payment": "0"
}
]
我已经创建了这个对象
public class EventDetails {
@SerializedName("id")
public String id;
@SerializedName("title")
public String name;
@SerializedName("price")
public String price;
@SerializedName("bookingFee")
public String booking_fee;
@SerializedName("standby")
public int isStandBy;
@SerializedName("subTitle")
public String show;
@SerializedName("closeTime")
public String time;
@SerializedName("openTime")
public String date;
@SerializedName("date")
public String eventDate;
//@SerializedName("genre")
public String[] genre;
@SerializedName("venueId")
public String venue_id;
@SerializedName("overview")
public String overview;
@SerializedName("djs")
public String presenter;
@SerializedName("terms")
public String terms;
@SerializedName("firstCutOff")
public String firstCutOff;
@SerializedName("secondCutOff")
public String secondCutOff;
@SerializedName("autoCutOff")
public String thirdCutOff;
@SerializedName("tickets")
public String tickets;
//@SerializedName("images")
public String[] image_url;
//@SerializedName("codes")
public String[] promoCodes;
}
我正试图使用GSON来解析这段代码,就像我在其他项目中使用这段代码所做的那样
String result = httpRequest(params);
Type collectionType = new TypeToken<List<EventDetails>>(){}.getType();
List<EventDetails> data = gson.fromJson(result, collectionType);
你有:
@SerializedName("terms")
public String terms;
您的JSON具有:
"terms": {
"ID Criteria": [
"Photo ID",
"Over 21s"
],
"Dress Code criteria": [
""
]
},
“术语”不是
字符串。它是一个对象(特别是一个包含两个字段的数组),Gson告诉您这一点 为嵌套的对象创建Java对象,然后添加到EventDetails类中
例如代码“代码”:[
{
“代码”:“HC0989”,
“价格”:“8.00”
}
],
您可以创建一个类
public class Codes{
public String code;
public String price;
}
然后添加到事件详细信息中
public Codes[] codes;
我刚刚注释掉了serialize语句,但仍然得到相同的错误,类似的错误也适用于“codes”字段。您需要将这些声明为对象字段,其中对象属于具有适当字段的类;在“代码”的情况下,它需要是一个适当类的列表。是的,我注意到,在你第一篇文章之后,我现在只将id和venueId序列化,并注释了所有其他内容以查看它是否有效,但我仍然得到相同的错误。你的大部分@SerializedName
注释都是多余的,实际上没有做任何事情;Gson与实际字段名匹配。使用该注释的唯一时间是JSON中的字段名与Java类中的字段名不同时。所以是的。。。如果Java中的某个字段在类型上仍然与您的JSON不匹配,那么您将不断收到错误,直到它匹配为止。更改名称以匹配我的JSON中的字段就成功了。为了他人的利益,请编辑您的答案以突出显示这一点。
public Codes[] codes;