-1

我从服务器收到了这个 Json 响应

[
  {
    "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);

我不断收到此异常,不确定解决方案是什么

08-17 19:39:00.835: W/System.err(29478): Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 31
4

2 回答 2

1

你有:

@SerializedName("terms")
public String terms;

你的 JSON 有:

"terms": {
    "ID Criteria": [
        "Photo ID",
        "Over 21s"
    ],
    "Dress Code criteria": [
        ""
    ]
},

“条款”不是String. 它是一个对象(特别是一个具有两个数组字段的对象),Gson 告诉你这一点。

于 2013-08-17T18:58:55.380 回答
0

为那些嵌套创建 Java 对象,然后添加到 EventDetails 类。

例如代码"codes": [ { "code": "HC0989", "price": "8.00" } ],

你可以创建一个类

public class Codes{
    public String code;
    public String price;
}

然后添加到您的 EventDetails

public Codes[] codes;
于 2013-08-21T09:46:54.953 回答