【问题标题】:How to deserialize this json with Gson?如何用 Gson 反序列化这个 json?
【发布时间】:2014-10-03 21:13:01
【问题描述】:

我将用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());
        }

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

对不起,我复制粘贴错了,但问题仍然存在,我的广告列表包含广告,但广告的每个字段都是空的

【问题讨论】:

  • 您的 json 格式不正确。尝试在这里验证它:jsonlint.com
  • 不根据 jsonlint.com
  • 好了,现在生效了!把你的代码放在这里:jsoneditoronline.org在右边你会看到你的对象的结构,所以很容易在java类中翻译它。
  • 对不起,我在问题中复制粘贴错误,现在有效

标签: java android json jackson gson


【解决方案1】:

让我们分析您的 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字符串映射到JavaString,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;
} 

完成对象树。

【讨论】:

  • 感谢您的帮助(+1),但我不明白“您没有 POJO 存在。您需要类似的东西”那么我的 Ad 类是什么?
  • @mmlooloo 您的Ad 类对应于键值为"ad" 的键值对中的JSON 对象。但是您没有包含该键值对的 Java 对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多