3

I am trying to convert JSON objects to POJO's with GSON. Although I dont get any errors/exceptions the List object in the wrapper class is still null in the end. Any idea's what i'm doing wrong

JSON String

 { "location":[ { "id":"1", "locationName":"Location 1", "eventType":[ { "id":"1", "eventName":"Event 1" }, { "id":"2", "eventName":"Event 2" }, { "id":"3", "eventName":"Event 3" } ] }, { "id":"2", "locationName":"Location 2", "eventType":[ { "id":"4", "eventName":"Event 4" }, { "id":"5", "eventName":"Event 5" }, { "id":"6", "eventName":"Event 6" } ] }, { "id":"3", "locationName":"Location 3", "eventType":[ { "id":"7", "eventName":"Event 7" }, { "id":"8", "eventName":"Event 8" }, { "id":"9", "eventName":"Event 9" } ] } ] } 

The Wrapper class used with GSON

public class LocationWrapper { public List<Location> locationList; public List<Location> getLocationList() { return locationList; } public void setLocationList(List<Location> locationList) { this.locationList = locationList; } } 

Location POJO

public class Location { private long id; private String locationName; private List<EventType> eventTypeList; public Location() { } public Location(long id, String locationName, ArrayList<EventType> eventTypeList) { this.id = id; this.locationName = locationName; this.eventTypeList = eventTypeList; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLocationName() { return locationName; } public void setLocationName(String locationName) { this.locationName = locationName; } public List<EventType> getEventTypeList() { return eventTypeList; } public void setEventTypeList(List<EventType> eventTypeList) { this.eventTypeList = eventTypeList; } } 

EventType POJO

public class EventType { private long id; private String eventName; public EventType() { } public EventType(long id, String eventName) { this.id = id; this.eventName = eventName; } public EventType(int id, String eventName) { this.id = id; this.eventName = eventName; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getEventName() { return eventName; } public void setEventName(String eventName) { this.eventName = eventName; } } 

The method I'm using

private void parseGSONfile(String fileName) { Gson gson = new Gson(); //getting string from file, you can insert the above string here String json = new JSONParser().getJSONStringFromFile(fileName); List<Location> locationList; LocationWrapper locationWrapper = null; try { locationWrapper = gson.fromJson(json, LocationWrapper.class); } catch (Exception e) { } //here the contained object locationList is still null locationList = locationWrapper.getLocationList(); } 

1 Answer 1

2

Your json provide a value for field location but your class LocationWrapper got a field named locationList, thus that does not match. Rename field or use @SerializedName.

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

1 Comment

Yep thats it, I also had an identical error in the Location class in the List<EventType>. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.