簡體   English   中英

如何使用Gson反序列化此json?

[英]How to deserialize this json with Gson?

我將使用Gson反序列化此json string

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

我為類創建的是(已刪除getter 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());
        }

但是我的列表包含objects(Ad),但它們的每個字段均為空,例如,它有3個廣告,但id,title,...全部為null,我該怎么辦?

抱歉,我復制並粘貼錯誤,但是問題仍然存在,我的廣告列表包含廣告,但廣告的每個字段都為空

讓我們分析一下您的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;
} 

完成對象樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM