2013-08-17 189 views
-1

我得從服務器JSON解析與GSON錯誤

[ 
    { 
    "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" 
    } 
] 

此JSON響應和我創建這個對象

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 

回答

0

爲嵌套的對象創建Java對象,然後添加到您的EventDetails類。

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

public Codes[] codes; 
+0

這對我有效 – GrayStatic

1

您有:

@SerializedName("terms") 
public String terms; 

您的JSON有:

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

「條款」不是String。這是一個Object(特別是一個帶有兩個數組的字段),Gson告訴你。

+0

我只是註釋掉進行序列化的說法,您可以創建一個類

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

然後添加到您的EventDetails但我仍然得到同樣的錯誤 – GrayStatic

+0

什麼是正確的方式來序列化這種類型的JSON? – GrayStatic

+0

類似的錯誤也適用於「代碼」字段。您需要將它們聲明爲對象字段,其中對象是具有適當字段的類;在「代碼」的情況下,它將需要一個適當的類的列表。 – Jules