繁体   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