aitkotw
aitkotw

Reputation: 9

How to convert Dynamic nested JSON to JAVA Object in android with GSON?

I'm trying to display following JSON to my RecyclerView and perform action accordingly. Now, the problem is, How can I access each key from JSON object and values inside keys. Do I need to store them in Java Objects with POJO or Array? I'm getting following response from server so I don't want to store them statically, I want to store it dynamically.

I looked for solution for the same but most of them include JSONArray and my response doesn't include any JSONArray. It includes key and JSONObject as a value. I don't know java as well, I am webdev, I've been looking for it for a day now.

{
"response": {
    "status": 200,
    "result": {
        "17": {
            "key1": "3",
            "key2": "111111",
            "key3": "aaa",
            "key4": "sss",
            "key5": "[email protected]",
            "key6": "1122334455",
            "key7": "2017-12-11",
            "key7": "something",
            "key9": "location"
        },
        "18": {
            "key1": "3",
            "key2": "111111",
            "key3": "aaa",
            "key4": "sss",
            "key5": "[email protected]",
            "key6": "1122334455",
            "key7": "2017-12-11",
            "key7": "something",
            "key9": "location"
        },
        "19": {
            "key1": "3",
            "key2": "111111",
            "key3": "aaa",
            "key4": "sss",
            "key5": "[email protected]",
            "key6": "1122334455",
            "key7": "2017-12-11",
            "key7": "something",
            "key9": "location"
        }
    }
  }
}

I have Result JSONObject in a string,

  public void onResponse(JSONObject response) {
                String status = null;
                try {
                    JSONObject responseCode = response.getJSONObject("response");
                    status = responseCode.getString("result");
                    //Todo

                } catch (JSONException e) {
                    e.printStackTrace();
                    defaultView.setText("Json Error");
                }

                defaultView.setText(status);

            }

Upvotes: 0

Views: 1615

Answers (3)

pirho
pirho

Reputation: 12215

Firstly I guess your example "key7": "something" should actually be "key8..".

Gson can parse any valid JSON to a LinkedTreeMap that you can then loop any way you want. However, your response seems to be static to some extent so you can create a DTO to make accessing values a bit easier. This DTO could be like:

@Getter
public class ResponseWrapper {
    @Getter
    public static class Response {
        private Integer status;
        private Map<Integer, Map<String, Object>> result;       
    }
    private Response response;
}

With this kind of a DTO you could then do foo example (and assuming no Java8 & Streams):

Response rw = new Gson().fromJson(RESPONSE_JSON, ResponseWrapper.class).getResponse();
Set<Entry<Integer, Map<String, Object>>> entries = rw.getResult().entrySet();
for(Entry<Integer, Map<String, Object>> e : entries) {
    Object o = e.getValue().get("key1");
    // do somethigs
}

If your example's result keys (17,18,19) contain a fixed set of keys - so just from key1 to key9 with the same data types - you could create a separate DTO for that also, like:

@Getter
public class ResultItem {
    private Long key1;
    private Long key2;
    private String key3;
    private String key4;
    private String key5;
    private Long key6;
    private Date key7;
    private String key8;
    private String key9;
}

and then your access to values might be just a bit more type safe, for example:

for(Entry<Integer, ResultItem> e : entries) {
    Long longKey1 = e.getValue().getKey1();
    // do somethings
}

Upvotes: 1

Viraj S
Viraj S

Reputation: 400

You have a response of result object with multiple object values.
So, you can simply apply a logic behind like,

JSONObject responseCode = response.getJSONObject("response");  
JSONObject resultCode = responseCode.getJSONObject("result");

    if(resultCode.length >0){
        for(int i=0;i<resultCode;i++){
            JSONObject listCode = resultCode.getJSONObject(i);
            String keyVal_1=listCode.getString("key1");
//store keyVal_1 in your arrayList and other keys 
//it will work till the dynamically generated last object you store.
        }
    }

Upvotes: 0

Hamdi Hanbali
Hamdi Hanbali

Reputation: 12

at the begging you can need to create your POJO's ,this site to create your POJO JsonToPojoLink

then you can use GSON like this:

   Gson gson = new Gson();
    ResponsePOJO responsePOJO;
    responsePOJO= gson.fromJson(response.toString(),ResponsePOJO.class);

Upvotes: 0

Related Questions