1

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 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.

Community
  • 1
  • 1
S.Fabio
  • 19
  • 3
  • I don't understand your question, every things seem fine to me. What are you trying to do ? – Benoit Vanalderweireldt Mar 25 '16 at 02:51
  • I want to be able to grab all the values and store them in a database eventually. I can grab the meetingNames using m.getMeetingName() however I cannot grab ShowCodes (meetingCodes, scheduledTypes). If I try to use m.getShowCode(), it returns a result like this packagename.ShowCode@210366b4 – S.Fabio Mar 25 '16 at 03:14
  • It returns you an instance of ShowCode, ShowCode has no toString method that's why you see this weird String. Try with m.getShowCode().getMeetingCode() – Benoit Vanalderweireldt Mar 25 '16 at 03:22
  • Wow thank you. haha so simple and it worked. I'm still learning java and had tried m.getShowCode.getMeetingCode() and since that failed I thought maybe I couldn't call it from my main class. – S.Fabio Mar 25 '16 at 03:31
  • Am I declaring showPools correctly? When I use `m.getExoticPools().getshowProduct()` It comes up with the error _'Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: '_ Is `private ShowPools[] showPools;` The wrong datatype? – S.Fabio Mar 25 '16 at 04:29

2 Answers2

1
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;
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
  • I tried using `private List showPools;` . It accepts it as a valid datatype however when I use this method `m.getShowCode().getShowProduct()` it doesnt recognise the method and leaves the error _Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: _ – S.Fabio Mar 25 '16 at 05:06
1

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
Mahendra
  • 1,436
  • 9
  • 15