java - 如何用Gson反序列化这个json?

标签 java android json jackson gson

我将使用 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"
}

我为类创建的是(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

{ // 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;
} 

完成对象树。

关于java - 如何用Gson反序列化这个json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186875/

相关文章:

javascript - 根据 d3.js 中的 JSON 属性选择特定的 svg 路径

python - 数据从 json 文件到 postgres 表

java - View 设置为 GONE 时不调用 Android onScrolled

java - 在 LinkedHashMap 中使用双向链表而不是单链表

Android webview 返回 navigator.online true

java - Android:找不到这个imageView?

java - Maven - 如何包含空目录

java - 覆盖更大线的最小线段数

android - Cordova 错误 : Dex: Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code

android - 截击网络 ImageView 和请求 header