0

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.

1

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had to make one wrapper as the top level in JSON is shown as an object. So created a wrapper class with List of feed as an element and applied your suggestion. It worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.