简体   繁体   中英

How to deserialize this json with Gson?

I am going to deserialize this json string with 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"
}

and what I have create for classes is (getter setter has been removed) :

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

and

public class AdsListResponse {

    @SerializedName("ads")
    private List<Ad> mAds;
    @SerializedName("message")
    private String mMessage;
    @SerializedName("status")
    private String mStatus;
}

and for converting I use below code

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

But my list contains objects(Ad) but every field of them is null, for example it has 3 ad but id,title,... are all null, what should I do?

Sorry I copied and paste it wrongly, but still the problem is there, my list of Ad contains Ad but each field of Ad is null

Let's analyze your 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
        }
    },

Considering that a JSON object maps to a Java POJO, a JSON string maps to a Java String , a JSON array maps to a Java array or Collection , and keys maps to fields, let's analyze your POJO classes

public class AdsListResponse { // root is an object

    @SerializedName("ads")
    private List<Ad> mAds; // field of type List (Collection) mapping to the array

So you've mapped

{ // an object at the root
"ads": [ // a key value pair, where the value is an array
    { 

The next token is

"ad": { // a key value pair, where the value is an object

You have no POJO where this exists. You'd need something like

public class AdWrapper {
    @SerializedName("ad")
    private Ad mAd;

and then change the AdsListResponse to

class AdsListResponse {    
    @SerializedName("ads")
    private List<AdWrapper> mAds;
} 

to complete the object tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM