如何用Gson反序化这个json?

时间:2014-10-03 21:13:02

标签: java android json jackson gson

我将使用json string

对此Gson进行反序列化
       {
    "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());
        }

但我的列表包含对象(广告),但它们的每个字段都为空,例如它有3个广告,但是id,title,...都是null,我该怎么办?

抱歉,我错误地复制并粘贴了它,但问题仍然存在,我的广告列表中包含广告但广告的每个字段都为空

1 个答案:

答案 0 :(得分: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;
} 

完成对象树。