0

I have a JSoN data like this:

{
   "data": {
      "noofCity": "1",


      "City 1": [
         {
            "id": "12",
            "title": "Delhi"
         }
      ]
   },
   "success": true
}

Now based on noofCity next tag City 1 will be generated. If noofCity will be 2 then there are two tag City 1 and City 2. Then how can I parse it using Json? Please tell me how can I generate my POJO class structure.

14
  • If noofCity has value 2 will you have two arrays City 1 and City 2? Is that right? Commented Apr 30, 2015 at 11:32
  • @konrad Krakowiak yes and also city1 and city 2 will again an array Commented Apr 30, 2015 at 11:36
  • you have to put "City 1" and "2". if "City 2" doesn't found, json parser ignores it and "City 2" reference will be null. (and use Gson) Commented Apr 30, 2015 at 11:39
  • @wisemann I guess he is not only concern about "City 1" and "City 2". He is concern about handling it dynamically because this list may grow beyond that. Commented Apr 30, 2015 at 11:43
  • @kunu you are right .. this list can be grow according to noofCity. Commented Apr 30, 2015 at 11:44

1 Answer 1

1

Your POJOs should look like below:

Main POJO for Response:

public class Response {

    Data data;

    boolean success;
}

For Data

public class Data {

    int noofCity;
    Map<String, List<City>> cityMap;


    void put(String key, List<City> city){
        if(cityMap == null){
            cityMap = new HashMap<>();
        }
        cityMap.put(key, city);
    }


    public void setNoofCity(int noofCity) {
        this.noofCity = noofCity;
    }

    public int getNoofCity() {
        return noofCity;
    }
}

For City

public class City {
    int id;
    String title;
}

But one of the most important think is a way how to deserialise Data. You have to prepare your own deserialiser for this, and define way how to fill HashMap as is shown in the code below:

public class DataDeserializer implements JsonDeserializer<Data> {

    @Override
    public Data deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Data result  = new Data();
        Gson gson = new Gson();
        JsonObject jsonObject=  json.getAsJsonObject();
        result.setNoofCity(jsonObject.get("noofCity").getAsInt());

        for(int i =1; i<=result.getNoofCity() ; i++ ){
           List<City> cities=  gson.fromJson(jsonObject.getAsJsonArray("City "+ i), List.class);
            result.put("City "+ i, cities);
        }
        return result;
    }
}

And now you can deserialise you json

 Gson gson = new GsonBuilder()
            .registerTypeAdapter(Data.class, new DataDeserializer())
            .create();
 Response test = gson.fromJson(json, Response.class);
Sign up to request clarification or add additional context in comments.

1 Comment

Krakowiask .. thanks for the answer...your answer works fine in my case. thanks again....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.