2014-10-03 26 views
0

我要反序列化這個json stringGson如何用Gson反序列化這個json?

 { 
    "ads": [ 
     { 
      "ad": { 
       "id": 1, 
       "title": "...", 
       "publicImage": "...", 
       "publicUrl": "...", 
       "adposition": { 
        "name": "home" 
       }, 
       "ttl": 30, 
       "debug": false 
      } 
     }, 
     { 
      "ad": { 
       "id": 2, 
       "title": "...", 
       "publicImage": "...", 
       "publicUrl": "...", 
       "adposition": { 
        "name": "splash" 
       }, 
       "ttl": 30, 
       "debug": false 
      } 
     }, 
     { 
      "ad": { 
       "id": 3, 
       "title": "...", 
       "publicImage": "...", 
       "publicUrl": "...", 
       "adposition": { 
        "name": "home" 
       }, 
       "ttl": 30, 
       "debug": false 
      } 
     } 
    ], 
    "message": "issue list generated", 
    "status": "success" 
} 

和我有什麼創建類是(吸氣setter方法已被刪除):

public class Ad { 

    public class AdPosition{ 

     private String mName; 

     public String getName() { 
      return mName; 
     } 
     public void setName(String name) { 
      mName = name; 
     } 

    } 

    @SerializedName("id") 
    private int mID; 
    @SerializedName("title") 
    private String mTitle; 
    @SerializedName("publicImage") 
    private String mPublicImage; 
    @SerializedName("publicUrl") 
    private String mPublicUrl; 
    @SerializedName("ttl") 
    private int mTtl; 
    @SerializedName("adposition") 
    private AdPosition mAdPosition;  
} 

public class AdsListResponse { 

    @SerializedName("ads") 
    private List<Ad> mAds; 
    @SerializedName("message") 
    private String mMessage; 
    @SerializedName("status") 
    private String mStatus; 
} 

和轉換我用下面的代碼

 Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
     AdsListResponse ads = gson.fromJson(response, AdsListResponse.class); 

     for(Ad ad : ads.getAds()){ 
      if(ad.getAdPosition().getName().equalsIgnoreCase("splash")){ 
       System.out.println(ad.getAdPosition().getName()); 
     } 

但我的列表包含對象(廣告),但它們的每個字段都爲空,例如它有3個廣告但id,title,...都是null,我該怎麼辦?

對不起我複製並錯誤地粘貼,但仍然問題是存在的,我的廣告列表中包含廣告,但廣告的每個字段爲空

+0

你的json格式不正確。嘗試驗證它在這裏:http://jsonlint.com/ – GVillani82 2014-10-03 21:14:49

+0

不根據jsonlint.com – zgc7009 2014-10-03 21:20:57

+1

好吧,現在它是有效的!把你的代碼放在這裏:http://www.jsoneditoronline.org/在右邊你會看到你的對象的結構,所以在java類中很容易翻譯它。 – GVillani82 2014-10-03 21:23:02

回答

3

讓我們來分析你的JSON

{ // an object at the root 
"ads": [ // a key value pair, where the value is an array 
    { // an object within the array 
     "ad": { // a key value pair, where the value is an object 

      // bunch of key value pairs, with int, string, boolean or object values 
      "id": 1, 
      "title": "...", 
      "publicImage": "...", 
      "publicUrl": "...", 
      "adposition": { 
       "name": "home" 
      }, 
      "ttl": 30, 
      "debug": false 
     } 
    }, 

考慮JSON對象映射到Java POJO,JSON字符串映射到Java String,JSON數組映射到Java數組或Collection,並且鍵映射到字段,讓我們分析您的POJO類

public class AdsListResponse { // root is an object 

    @SerializedName("ads") 
    private List<Ad> mAds; // field of type List (Collection) mapping to the array 

所以你映射

{ // an object at the root 
"ads": [ // a key value pair, where the value is an array 
    { 

下一個標記是

"ad": { // a key value pair, where the value is an object 

你沒有POJO這個地方存在。你會需要像

public class AdWrapper { 
    @SerializedName("ad") 
    private Ad mAd; 

,然後更改AdsListResponse

class AdsListResponse {  
    @SerializedName("ads") 
    private List<AdWrapper> mAds; 
} 

完成對象樹。

+0

感謝您的幫助(+1),但我不明白「你有沒有POJO存在這種情況,你需要類似」所以我是什麼廣告類? – mmlooloo 2014-10-03 21:41:08

+1

@ mmlooloo您的'Ad'類對應於鍵值爲「ad」的鍵值對中的JSON對象。但是你沒有Java對象來包含該鍵值對。 – 2014-10-03 21:42:55

+0

非常感謝兄弟! – mmlooloo 2014-10-03 21:44:56