0

I have a json array containing the list of widgets. But each widget has different data on the basis of the it's type. I need to parse this JSON by using the GSON android library. The approach i followed is : i have taken on response model containing the list of WidgetModel. The WidgetModel is of typed type. I have also defined the various types of data for the type. For this i have created a gson deserializer but it is not working for me.

please check the json given below:

{
  "page_title": "Invite And Earn",
  "widgets": [
    {
      "type": "image",
      "index": 0,
      "title": null,
      "ic_link": null,
      "data": [
        {
          "link": "https://www.dike.li.us/images/sample-1.jpg"
        }
      ]
    },
    {
      "type": "text",
      "index": 1,
      "title": null,
      "ic_link": null,
      "data": [
        {
          "text": "Invite your friends. "
        }
      ]
    },
    {
      "type": "ref_code",
      "index": 2,
      "title": null,
      "ic_link": null,
      "data": [
        {
          "label": "Your Invite Code:",
          "code": "AMANOV8KR"
        }
      ]
    },
    {
      "type": "line",
      "title": null,
      "ic_link": null,
      "index": 3,
      "data": [
        {
          "style": "full"
        }
      ]
    }
  ]
}

Deseirializer code is :

public class GsonDeserializer implements JsonDeserializer<WidgetRespModal> {

    @Override
    public WidgetRespModal deserialize(JsonElement json, Type typeOfT,
                                       JsonDeserializationContext context) throws JsonParseException {
        Type type = null;
        switch (json.getAsJsonObject().get("type").getAsString()) {
            case WidgetRespModal.WidgetType.IMAGE_WIDGET:
                type = new TypeToken<ArrayList<ImageWidgetConfig>>() {
                }.getType();
                break;
            case WidgetRespModal.WidgetType.LINE_WIDGET:
                type = new TypeToken<ArrayList<LineWidgetConfig>>() {
                }.getType();
                break;
            case WidgetRespModal.WidgetType.LINK_WIDGET:
                type = new TypeToken<ArrayList<LinkWidgetConfig>>() {
                }.getType();
                break;

            case WidgetRespModal.WidgetType.TEXT_WIDGET:
                type = new TypeToken<ArrayList<TextWidgetConfig>>() {
                }.getType();
                break;
        }
        WidgetRespModal respModal = new WidgetRespModal();
        if(json.isJsonObject()){
            JsonObject respJson = json.getAsJsonObject();
            respModal.setType(respJson.get("type").getAsString());
            respModal.setIndex(respJson.get("index").getAsInt());
            respModal.setIconLink(respJson.get("ic_link").getAsString());
            respModal.setTitle(respJson.get("title").getAsString());
            respModal.setConfig((List) context.deserialize(respJson.get("data").getAsJsonArray(), type));

        }
        return respModal;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29

2 Answers2

0

Use Gson

Type type = new TypeToken<List<Student>>(){}.getType();     
List<Student> students = gson.fromJson(json, type);

For further information check the post

https://stackoverflow.com/a/15011927/6742601

Fazal Hussain
  • 1,129
  • 12
  • 28
0

There is no point of creating deserializer for this simple use case.

Following line is enough new Gson().toJson(strResponse, WidgetRespModal.class);