1

This is how my JSON looks like and I have to parse the JSON, how can this be done using GSON

{
  "data": {
    "a": {
      "abc": {
        "c": "d"
      }
    }
  }
}

In which "a" is dynamic key which may vary from time to time. I am unable to find a solution right now

CSG0811
  • 632
  • 5
  • 20
  • Use Iterator for that – IntelliJ Amiya Apr 05 '17 at 07:09
  • a custom deserializer might help https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer http://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Rohit Arya Apr 05 '17 at 07:10
  • Possible duplicate of [Gson: Is there an easier way to serialize a map](http://stackoverflow.com/questions/8360836/gson-is-there-an-easier-way-to-serialize-a-map) – Lyubomyr Shaydariv Apr 05 '17 at 07:38
  • you may change the SerializedName annotation value at run-time in your model class http://stackoverflow.com/questions/14268981/modify-a-class-definitions-annotation-string-parameter-at-runtime – H4SN Apr 05 '17 at 08:29

2 Answers2

2

Model

public class Model {

    private HashMap<String, String> data;

    public Model() {
    }
}

Convert json string to Hashmap using Gson & prepare data from hashmap

Gson gson = new Gson();
Type typeHashMap = new TypeToken<Map<String, String>>(){}.getType();
Map<String,String> map = gson.fromJson(YOUR_JSON, typeHashMap);

Set<Map.Entry<String, String>> entrySet = data.entrySet();

    Iterator iterator = entrySet.iterator ();

    for(int j = 0; j < entrySet.size(); j++) {
        try {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            //Add it to your list
        }
        catch(NoSuchElementException e) {
            e.printStackTrace();
        }
        break;
    }
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • It can be, I am updating answer in a while – PEHLAJ Apr 05 '17 at 07:34
  • Thanks for your answer, but I am using retrofit to make this API call which needs a response model so I am looking for a way to make a model class to parse this response can u guide me through that please? – CSG0811 Apr 05 '17 at 07:56
  • You can convert it to model as well – PEHLAJ Apr 05 '17 at 07:57
  • Which code? Java or the response structure you want? – CSG0811 Apr 05 '17 at 08:05
  • My Model Class looks like this public class Model { @SerializedName("data") @Expose private Map data; } and InnerModel is what contains serialization of "abc" I am able to get "a" which is dynamic by this way but unable to get the inner data model. When I tried to hit the API in postman got a valid response but in the app I am getting all parsed values as null and unable to understand the reason behind that – CSG0811 Apr 05 '17 at 08:18
  • i am not able to get the values from the model class... will u please explain this answer again... – amit pandya Dec 12 '17 at 14:16
1

I am not sure if internal part abc is known to you or not. If it is known to you then you can surely do it with GSON. You have to create the class for the inner known object as below:

public class ABC {
public C abc;}

Then create the Class for C:

public class C {
public String c;}

Then just pass the ABC class as the hashmap value as below:

public HashMap<String, ABC> a;
Siraj Sumra
  • 934
  • 1
  • 11
  • 28
  • yes internal part is know to me. My Model Class looks like this public class Model { @SerializedName("data") @Expose private Map data; } and InnerModel is what contains serialization of "abc" I am able to get "a" which is dynamic by this way but unable to get the inner data model. When I tried to hit the API in postman got a valid response but in the app I am getting all parsed values as null and unable to understand the reason behind that This is what I have done – CSG0811 Apr 05 '17 at 09:51