Saikat
Saikat

Reputation: 16710

Unable to convert Gson JsonObject to POJO class

I have a simple POJO class Defect:

    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;

    @Setter
    @Getter
    @ToString
    public class Defect {

        private String formattedId;
        private String fixedInBuild;
        private String foundInBuild;
        private String verifiedInBuild;
        private String state;
        private String resolution;
        private String submittedBy;
        private String priority;
        private String severity;    
    }

And I am trying to convert a Gson.JsonObject to Defect. The JsonObject looks like this:

enter image description here

I want to store only specific fields hence not all added in the POJO.

But new GsonBuilder().create().fromJson(object, Defect.class) returns a Defect object with all null values. What is wrong here?

Upvotes: 0

Views: 297

Answers (1)

Ratish Bansal
Ratish Bansal

Reputation: 2442

Your json elements are different from the POJO members as a result proper mapping is not happening. Please annotate formattedId as

@SerializedName("FormattedID")
private String formattedId;

to make formattedId work.

Upvotes: 3

Related Questions