Deserialize a complex Json object using GSON in Java

advertisements

Using this as a reference I have described the structure of my Json data and can grab the information as needed until I get to nest records and arrays. Parsing a complex Json Object using GSON in Java

However my JSON data is nested several times over. For example;

{
  "meetings": [
    {
      "meetingName": "GaryVon",
      "location": "USA",
      "meetingType": "P",
      "meetingDate": "2016-03-25",
      "weatherCondition": "FINE",
      "showCode": {
        "meetingCode": "A",
        "scheduledType": "R"
      },
      "venueType": "ANI",
      "showPools": [
        {
          "showProduct": "GaryVon",
          "showStatus": "Open",

        }
      ]
    }
  ]
}

I have my wrapper and classes describing the format of the json data. Each class in a new java file.

public class meetingContainer {
    public List<meetings> meetings;
}

Top level class

public class meetings {
   private String meetingName;
   private String location;
   private String meetingType;
   private String meetingDate;
   private String weatherCondition;
   private ShowCode showCode;
   private String venueType;
   private ShowPools[] showPools;

   public String getMeetingName() { return meetingName; }
   public String getLocation() { return location; }
   public String getMeetingType() { return meetingType; }
   public String getMeetingDate() { return meetingDate; }
   public String getWeatherCondition() { return weatherCondition; }
   public ShowCode getShowCode() { return showCode; }
   public String getVenueType() { return venueType; }
   public ShowPools[] getShowPools() { return showPools; }
}

2nd Level class

public class ShowCode {
   private String meetingCode;
   private String scheduledType;

   public String getMeetingCode() { return meetingCode; }
   public String getScheduledType() { return scheduledType; }
}

2nd Level Class

public class ShowPools {
   private String showProduct;
   private String showStatus;

   public String getShowProduct() { return showProduct; }
   public String getShowStatus() { return showStatus; }
}

I then try to parse it and grab the data which works fine until I get into nested arrays/records

Gson g = new Gson();
meetingContainer mc = g.fromJson(jsonMeetingsString, meetingContainer.class);
for(meetings m: mc.meetings){
    System.out.println(m.getMeetingName()); //Result = "GaryVon"
    System.out.println(m.getLocation()); //Result = "USA"
    System.out.println(m.getmeetingType()); //Result = "P"
    System.out.println(m.getShowCode());  //Result = "[email protected]"
}

My question is how to I declare nested arrays/records and then call those methods from different classes i.e. Call the methods in showcode and showpools. The other post did not say how. Sorry if this is a simple answer as I'm new to java.


m.getShowCode()

This returns a reference of type ShowCode, to access inner values use the getters, for example :

m.getShowCode().getMeetingCode()


You should use a list for showPools

private List<ShowPools> showPools;