Kumar Gaurav
Kumar Gaurav

Reputation: 1327

Parsing array of object using Gson

My Rest service produces response as below

{
    "feeds": [
        {
            "id": 672,
            "imagePath": "http://pixyfi.com/uploads/image1.jpg",
            "description": "Off White Cotton Net^The Dress Is Made From Cotton Net. It Is Stretchable And The Material Is Really Good. It Is A Bodycon Dress.",
            "uploader": {
                "id": 459,

            },
            "rejected": false,
            "moderator": {
                "id": 95,
            },
            "moderatedOn": "2016-12-19"
            "imagePaths": [
                "uploads/image1.jpg"
            ]
        },
        {
            "id": 672,
            "imagePath": "http://pixyfi.com/uploads/mage2.jpg",
            "description": "Off White Cotton Net^The Dress Is Made From Cotton Net. It Is Stretchable And The Material Is Really Good. It Is A Bodycon Dress.",
            "uploader": {
                "id": 459,

            },
            "rejected": false,
            "moderator": {
                "id": 95,
            },
            "moderatedOn": "2016-12-19"
            "imagePaths": [
                "uploads/image2.jpg"
            ]
        }
    ]
}

How can i parse it with Gson. IN my android client also i have same Feed Class witch which this JSON was generated.
Note: I have used Spring boot for my rest API and this JSON was generated with ResponseEntity.

Upvotes: 0

Views: 1177

Answers (1)

Hobbit
Hobbit

Reputation: 621

Firstly, make sure that you have got valid JSON. The above in your case is not valid.

If a json object contains a single element, then there is no need to place comma after that. (comma after id in moderator and uploader object). You need to remove that.Also you need to place a comma after moderatedOn value.

Now after you got valid one, you have a feed class. In order to map your json feeds Array onto your List. You need to do the following.

Gson gson = new Gson();

Type feedsType = new TypeToken<ArrayList<Feed>>(){}.getType();

List<Feed> feedList = gson.fromJson(yourJsonResponseArray, feedsType); 

Your Classes are must be like these. Feed Class

public class Feed
{
    private String id;
    private String imagePath;
    private Moderator moderator;
    private String description;
    private String rejected;
    private Uploader uploader;
    private String moderatedOn;
    private String[] imagePaths;
    public String getId ()
    {
        return id;
    }
    public void setId (String id)
    {
        this.id = id;
    }
    public String getImagePath ()
    {
        return imagePath;
    }
    public void setImagePath (String imagePath)
    {
        this.imagePath = imagePath;
    }
    public Moderator getModerator ()
    {
        return moderator;
    }
    public void setModerator (Moderator moderator)
    {
        this.moderator = moderator;
    }
    public String getDescription ()
    {
        return description;
    }
    public void setDescription (String description)
    {
        this.description = description;
    }
    public String getRejected ()
    {
        return rejected;
    }
    public void setRejected (String rejected)
    {
        this.rejected = rejected;
    }
    public Uploader getUploader ()
    {
        return uploader;
    }
    public void setUploader (Uploader uploader)
    {
        this.uploader = uploader;
    }
    public String getModeratedOn ()
    {
        return moderatedOn;
    }
    public void setModeratedOn (String moderatedOn)
    {
        this.moderatedOn = moderatedOn;
    }
    public String[] getImagePaths ()
    {
        return imagePaths;
    }
    public void setImagePaths (String[] imagePaths)
    {
        this.imagePaths = imagePaths;
    }
}

Moderator Class

public class Moderator
{
    private String id;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }
}

Uploader Class

public class Uploader
{
    private String id;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }
}

Upvotes: 1

Related Questions