0

How can i parse following response from an api using Retrofit lib in Android?

{
  "elements": {
    "12": {
        ...
    },
    "23": {
        ...
    },
    "32": {
        ...
    }
  },

  "more": null
}

Here elements is an json object but is needed as array.

Here is what i am trying

public static class ElementResponseDeserializer implements JsonDeserializer<ElementResponse>{
        @Override
        public ElementResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            logger.d("JSON >>>> typeOfT" + typeOfT.toString());
            logger.d("JSON >>>> context" + context.toString());
            logger.d("JSON >>>> " + json.toString());
            JsonObject object = json.getAsJsonObject();
            object.get
            return new ElementResponse();
        }
    }

And using this way

Gson gson = new GsonBuilder()
           .registerTypeAdapter(ElementsResponse.class, new ElementsResponseDeserializer())
                    .create();

RestAdapter restAdapter = new RestAdapter.Builder()
       .setClient(new OkClient(client))
       .setEndpoint(URL_API)
       .setConverter(new GsonConverter(gson))
       .setRequestInterceptor(new ApiRequestInterceptor())
       .build();
restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
return restAdapter.create(MyApp.class);
Dhruv
  • 1,129
  • 2
  • 13
  • 32
  • what does *Here elements is an json object but is needed as array.* mean? – Blackbelt Dec 16 '14 at 11:24
  • @Blackbelt as you can see in the question elements is json object but it contains other json objects which are of same type so it is better taken as array but it isn't and can't be changed, still it is desired to get the output from retrofit lib as Array of Objects. – Dhruv Dec 16 '14 at 11:28
  • I think he means that ***elements*** could be and array but its provided as an object which contains a variable number of objects and it would be better represented if ***elements*** would be an array but he has no control on the representation so he's asking how to map that with retrofit – forcewill Dec 16 '14 at 12:03
  • Create an entity class, use Gson? This will work if the structure doesn't change drastically on a regular basis. – G_V Dec 16 '14 at 12:39
  • @G_V yes you're right but thats bit complax for me if you've done that please share, i had used gson but i am new to retrofit and JsonDeserializer – Dhruv Dec 16 '14 at 13:00
  • @G_V question updated with code have a look – Dhruv Dec 16 '14 at 13:10
  • Take a look at http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java – G_V Dec 16 '14 at 13:13
  • @G_V here the problem is how to get elements from jsonobject as if its JsonArray, it is the thing we don't have method like get(0) in JsonObject and we don't know the names on its children – Dhruv Dec 16 '14 at 13:46
  • If it's just an array of strings without any meaningful data that can be parsed without having to check anything because there's no data structure, I'm sure we can find some tutorials on that but I'm not sure what you're trying to do. – G_V Dec 16 '14 at 14:00

0 Answers0