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": "aaa@asd.com",
            "key6": "1122334455",
            "key7": "2017-12-11",
            "key7": "something",
            "key9": "location"
        },
        "18": {
            "key1": "3",
            "key2": "111111",
            "key3": "aaa",
            "key4": "sss",
            "key5": "aaa@asd.com",
            "key6": "1122334455",
            "key7": "2017-12-11",
            "key7": "something",
            "key9": "location"
        },
        "19": {
            "key1": "3",
            "key2": "111111",
            "key3": "aaa",
            "key4": "sss",
            "key5": "aaa@asd.com",
            "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);

            }

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);

How to parse a dynamic JSON key in a Nested JSON result?, java android json Another possibility is to use Gson (Note, I use lombok here to generates getters/setters, toString, etc): package so7304002; import java.util. using GSON, but instead of using GSON converter adapter to convert into to POJO. //parsing string response to json object JsonObject jsonObject = (​JsonObject)  Convert JSON to Java Object. Here’s an example on how you can convert json to java object. We start first by converting to simple POJO then we’ll also show you how you can do the same for Collections Object. Add Jackson Dependency. Add the dependency for JSON.

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.
        }
    }

Gson, How to convert Dynamic nested JSON to JAVA Object in android with GSON? - android. A quick guide to learn how to convert a Java Object to a JSON string or file and vice versa by using the Gson library. To offer you a better browsing experience, we use cookies. By using this site, you agree to our Cookies Policy .

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
}

JSON handling with org.json / Create nested JSON object / Essential , Gson — Mapping of Nested Objects. by Norman Peitek on May 26 2016 , tagged in Gson, Android, Java , 9 min read  Learn to use Google GSON library to deserialize or parse JSON, containing json array, to java array or java list objects. It’s worth mentioning that JSON has only array datatype . Java has both – arrays and lists .

JSON handling with org.json / Handling dynamic key for JSON , JSON handling with org.json / Create nested JSON object / Essential Android. Handling dynamic key for JSON response · Parsing json array to generic class using JSON SerializationDeserialization with AutoValue and Gson · Updating the 167 Speech to Text Conversion 199 Java Native Interface (JNI). 200 FileIO. We want to parse nested JSON of the generic type because these responses have the same wrapper. This article will share our final solution and how to convert all ResponseBody to Data<T>, and then

How to convert deeply nested JSON object to Java object, Java code. // ResponseData is raw string of response JSONObject responseDataObj = new JSON in Android with org.json Create nested JSON object · Parsing JSON to Generic Class Object with Gson 167 Speech to Text Conversion. In this tutorial, we will show you how to use Gson to convert Java object to / from JSON.. P.S All examples are tested by Gson 2.8.5

GSON Tutorial Part 2 - NESTED OBJECTS, GSON is an open source Java library to serialize and deserialize Java objects to (and from) JSON. It works Jackson parser is very slow for deeply nested objects. Kundan, former Front End Android App Developer at SmarterClinics (​2016). In part 2 of the GSON series we are going to serialize and deserialize a java object, which contains another java object as one of it's fields. This will result in a nested JSON object. Watch the

Comments
  • stackoverflow.com/a/6796987/2633909
  • Not a chance you're using Retrofit?