Rishabh Baranwal
Rishabh Baranwal

Reputation: 9

Json Deserialize for hetero-type json Java

Apologies in advance, I'm a little new to JSON parsing and I m facing a problem in parsing JSON in Object in java.

{ result: {
      "City": {
        "Delhi": {
          "A-Hospital": {
            "pincode": 400001
          },
          "B-Hospital": {
            "pincode": 400002
          },
          "C-Hospital": {
            "pincode": 400003
          },
          .
          .
          .
        },
        "Mumbai": {
          "A-Hospital": {
            "pincode": 500001
          },
          "B-Hospital": {
            "pincode": 500002
          },
          "C-Hospital": {
            "pincode": 500003
          },
          .
          .
          .
        },
        "Bangalore": {
          "A-Hospital": {
            "pincode": 600001
          },
          "B-Hospital": {
            "pincode": 600002
          },
          "C-Hospital": {
            "pincode": 600003
          },
          "D-Hospital": {
            "pincode": 600004
          },
          .
          .
          .
        }
      }
     }
    }

Receiving Json is received from 3rd person hence can't the format. How to create the class structure for such dynamic json and parse into Object ?

class City{
  private Map<String, Map<String,Hospital>> hours;
//Getter and Setter
}

class Hospital {
    private String pincode;
    //Getter and Setter
}

I want to form a Map of the City with Hospital(A-Hospital,B-Hospital,etc) as Object. Example: Map<String,Hospital> cityHospitalMapping. class Hospital { String HospitalName; Integer pincode; }

But How to write the Deserializer ? Tried

JsonObject allcities = (JsonObject) json.get("result");
City cities = new Gson().fromJson(allcities,City.class);

But cities is not containing any data.

Result:
cities{pincode=null}

Upvotes: 0

Views: 58

Answers (1)

pirho
pirho

Reputation: 12235

Edited because you added the result in your JSON. Your problem here - after edit - is that City you declared does not contain but it itself IS a Map<String, Map<String,Hospital>>.

First of all you need a "wrapper" class, say Data. That is because your JSON example has an object that contains Response that contains City. For City, there are no fixed field names so you need to deserialize it as a Map.

The same applies to values held in the map City. Values in City are also type of Map let us call these values inner map. Because there is this one fixed field name pincode you can declare before mentioned values in this inner map as of type Hospital, leading to a class like:

@Getter @Setter
public class Data {
    @Getter @Setter
    public class Result {
        private Map<String, Map<String, Hospital>> City;
        @Getter @Setter
        public class Hospital {
            private String pincode;
        }
    }
    private Result result;
}

Now if you deserialize with Gson using this above class you can use it like:

Data data = gson.fromJson(JSON, Data.class);
Map<String, Map<String, Hospital>> city = data.getResult().getCity();

and obtain 400002.

You could implement some more or less complex custom deserializer but maybe it is more easy to deserialize JSON as it is and after tath do some mappings to other types of classes if needed.

Upvotes: 1

Related Questions