简体   繁体   中英

Deserialize a complex Json Object using GSON in Java

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 = "packagename.ShowCode@210366b4"          
}

My question is how to I declare nested arrays/records and then call those methods from different classes ie 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;

Your provided JSON string is invalid. It has one extra , -

        {
          "showProduct": "GaryVon",
          "showStatus": "Open",
                              ^

Answer for your question you asked in comment : m.getShowCode().getShowProduct() is invalid since showCode node has only two attributes meetingCode and scheduledType .

below code is listing all values of JSON. Let me know if it not covers your question

    Gson g = new Gson();
    meetingContainer mc = g.fromJson(jsonMeetingsString,
            meetingContainer.class);
    for (meetings m : mc.meetings) {
        System.out.println("meetingName: " + m.getMeetingName()); 
        System.out.println("location: "+ m.getLocation()); 
        System.out.println("meetingType: "+ m.getMeetingType()); 
        System.out.println("meetingDate: "+ m.getMeetingDate()); 
        System.out.println("weatherConditio: "+ m.getWeatherCondition()); 
        System.out.println("showCode->meetingCode: "+ m.getShowCode().getMeetingCode()); 
        System.out.println("showCode->scheduledType: "+ m.getShowCode().getScheduledType());
        System.out.println("venueType: "+ m.getVenueType()); 
        for(ShowPools showPool : m.getShowPools()){
            System.out.println("showPools->showProduct: "+ showPool.getShowProduct());
            System.out.println("showPools->showStatus: "+ showPool.getShowStatus());
        }
    }

Output:

meetingName: GaryVon
location: USA
meetingType: P
meetingDate: 2016-03-25
weatherConditio: FINE
showCode->meetingCode: A
showCode->scheduledType: R
venueType: ANI
showPools->showProduct: GaryVon
showPools->showStatus: Open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM