I have this response json, which the keys are dynamic, sometimes I have only 3 items in "result" and sometime I have 5 or more. How do I have to write the class to deserialize it correctly? The key and value can be dynamic in name and number. i thought i just need a list?
Main Class
public static void main(String[] args) {
String response = "{\n" +
" \"error\": [],\n" +
" \"result\": {\n" +
" \"value11\": \"0.0233260\",\n" +
" \"value22\": \"59.2500\",\n" +
" \"value33\": \"0.0233260300\",\n" +
" \"value44\": \"0.00000000\"\n" +
" }\n" +
"}";
Gson gson = new Gson();
var responseBalance= gson.fromJson(response, ResponseIds.class);
System.out.println("Print " + responseBalance.getResult().getData());
}
ResponseIds Class
public class ResponseIds {
@SerializedName("result")
private Result result;
@SerializedName("error")
private List<Object> error;
public Result getResult(){
return result;
}
public List<Object> getError(){
return error;
}
}
Result Class
public class Result{
private HashMap<String, String> data;;
public HashMap<String, String> getData() {
return data;
}
public void setData(HashMap<String, String> data) {
this.data = data;
}
}
Edit: Ok this is a simple solution. Result itself is a map
public class ResponseIds {
@SerializedName("result")
private Map<String, String> result;
@SerializedName("error")
private List<Object> error;
public Map<String, String> getResult() {
return result;
}
}