0

I have following JSON string that I need to set to the Java objects of POJO class. What method should I follow?

 {"status":"FOUND","messages":null,"sharedLists":      [{"listId":"391647d","listName":"/???","numberOfItems":0,"colla   borative":false,"displaySettings":true}] }

I tried using Gson but it did not work for me.

Gson gson = new Gson();
SharedLists target = gson.fromJson(sb.toString(), SharedLists.class);

Following is my SharedLists pojo

 public class SharedLists {

@SerializedName("listId")
private String listId;

@SerializedName("listName")
private String listName;

@SerializedName("numberOfItems")
private int numberOfItems;

@SerializedName("collaborative")
private boolean collaborative;

@SerializedName("displaySettings")
private boolean displaySettings;

public int getNumberOfItems() {
    return numberOfItems;
}
public void setNumberOfItems(int numberOfItems) {
    this.numberOfItems = numberOfItems;
}
public boolean isCollaborative() {
    return collaborative;
}
public void setCollaborative(boolean collaborative) {
    this.collaborative = collaborative;
}
public boolean isDisplaySettings() {
    return displaySettings;
}
public void setDisplaySettings(boolean displaySettings) {
    this.displaySettings = displaySettings;
} 

public String getListId() {
    return listId;
}
public void setListId(String listId) {
    this.listId = listId;
}

} 
4
  • I think you need to classes to map this entire JSON response. You can generate it from jsonschema2pojo.org Commented May 16, 2017 at 16:55
  • Can you please clarify the above statement of yours? Commented May 16, 2017 at 17:57
  • I hope the key "colla borative" do not have space in your json string. Commented May 16, 2017 at 18:35
  • See my answer below with details Commented May 16, 2017 at 18:54

1 Answer 1

1

Following is your JSON string.

{
  "status": "FOUND",
  "messages": null,
  "sharedLists": [
    {
      "listId": "391647d",
      "listName": "/???",
      "numberOfItems": 0,
      "colla   borative": false,
      "displaySettings": true
    }
  ]
}

Clearly sharedLists is a JSON array within the outer JSON object.

So I have two classes as follows (created from http://www.jsonschema2pojo.org/ by providing your JSON as input)

ResponseObject - Represents the outer object

 public class ResponseObject {

        @SerializedName("status")
        @Expose
        private String status;
        @SerializedName("messages")
        @Expose
        private Object messages;
        @SerializedName("sharedLists")
        @Expose
        private List<SharedList> sharedLists = null;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public Object getMessages() {
            return messages;
        }

        public void setMessages(Object messages) {
            this.messages = messages;
        }

        public List<SharedList> getSharedLists() {
            return sharedLists;
        }

        public void setSharedLists(List<SharedList> sharedLists) {
            this.sharedLists = sharedLists;
        }

    }

and the SharedList - Represents each object within the array

public class SharedList {

    @SerializedName("listId")
    @Expose
    private String listId;
    @SerializedName("listName")
    @Expose
    private String listName;
    @SerializedName("numberOfItems")
    @Expose
    private Integer numberOfItems;
    @SerializedName("colla borative")
    @Expose
    private Boolean collaBorative;
    @SerializedName("displaySettings")
    @Expose
    private Boolean displaySettings;

    public String getListId() {
        return listId;
    }

    public void setListId(String listId) {
        this.listId = listId;
    }

    public String getListName() {
        return listName;
    }

    public void setListName(String listName) {
        this.listName = listName;
    }

    public Integer getNumberOfItems() {
        return numberOfItems;
    }

    public void setNumberOfItems(Integer numberOfItems) {
        this.numberOfItems = numberOfItems;
    }

    public Boolean getCollaBorative() {
        return collaBorative;
    }

    public void setCollaBorative(Boolean collaBorative) {
        this.collaBorative = collaBorative;
    }

    public Boolean getDisplaySettings() {
        return displaySettings;
    }

    public void setDisplaySettings(Boolean displaySettings) {
        this.displaySettings = displaySettings;
    }

}

Now you can parse the entire JSON string with GSON as follows

Gson gson = new Gson();
ResponseObject target = gson.fromJson(inputString, ResponseObject.class);

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

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.