Reputation: 643
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
Upvotes: 1
Views: 3241
Reputation: 954
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;
Upvotes: 1
Reputation: 10126
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;
}
Upvotes: 2