0

I'm trying to Parse a nested JSON Array using Gson and showing the array inside recyclerview. The way i'm parsing the JSON array doesn't seems okay/proper to me but its doing the job. But i want to know how to handle such cases properly.

Sample JSONArray

            {
             "Status":"success",
             "Response":[
                    {
                         "Title":"First Title",
                         "Code":"20210209",
                         "Content":[
                                {
                                     "field_1":"field_1_text",
                                     "field_2":true
                                },
                                {
                                     "field_1":"field_1_text",
                                     "field_2":true
                                }
                         ]
                    },
                    {
                         "Title":"Second Title",
                         "Code":"2021020902",
                         "Content":[
                                {
                                     "field_1":"field_1_text_2",
                                     "field_2":true
                                },
                                {
                                     "field_1":"field_1_text_2",
                                     "field_2":false
                                }
                         ]
                    }
             ]
        }

Parsing with Gson (focused only on Response array) :

         List<ResponseArrayObject> responseList = new ArrayList<>();
         Type listType = new TypeToken<ArrayList<ResponseArrayObject>>(){}.getType();
         responseList.addAll(new Gson().fromJson(response.toString(), listType));

Till here, I'm parsing the complete ResponseArray in one single main list. After this I've created a new List of different objecttype with Getter/Setter and iterate through responseList and if the Content.size() is greater than 0, I'm doing the following.

        for(ResponseArrayObject responseO:responseList){
        if(responseO.Content.size()>0){
           CustomResponseList.add(new CustomerResponse(responseO.Title,null));
           for(Response.Content c:responseO.Content){
              CustomResponseList.add(new CustomerResponse(null,c));
           }
        }
    }

This way i'm getting one list with different type of data i.e.

            [
            {
            "Title" : "First Title",
            "Content" : null;
            },{
            "Title" : null,
            "Content" : {
            "field_1": "field_1_text",
            "field_2": true
            },
            {
            "Title" : "null",
            "Content" : {
            "field_1": "field_1_text",
            "field_2": true
            },{
            "Title" : "Second Title",
            "Content" : null;
            },{
            "Title" : "null",
            "Content" : {
            "field_1": "field_1_text_2",
            "field_2": true
            },
            {
            "Title" : "null",
            "Content" : {
            "field_1": "field_1_text_2",
            "field_2": true
            }
            }
            ]

Now this one is simple Array with nested OBJECT and I can simple iterated through this array in recycerlview adapter without any issues and use different layout configs based on null value.

But i feel like it is not the right way. Please someone look into it and tell me how to do it properly.

A lot of links here on SO are basically parsing the Nested Arrays which i can do easily but no ones talking about how to use the parsed Nested arrays in Recyclerview Adapter.

Joe
  • 173
  • 1
  • 15
  • In your case `POJO` model does not fit to `JSON` payload. Take a look on [similar question](https://stackoverflow.com/questions/55248523/array-of-json-object-to-java-pojo/55249189#55249189) where I show how to use online tool: http://www.jsonschema2pojo.org. In your case also uncheck: `Include getters and setters` option. It will generate a wrapper class for the root `JSON Object` with `Response` field inside. – Michał Ziober Feb 09 '21 at 17:38
  • yes @MichałZiober, may be model won't fit the JSON Payload as I wrote it down here in SO for reference and understanding. but my main question is how to use the well formated well parsed nested Arrays in recyclerview adapter. – Joe Feb 09 '21 at 18:30

1 Answers1

0

can you try using in this way

List<ResponseArrayObject> responseList = new ArrayList<>();
Type listType = new TypeToken<ArrayList<ResponseArrayObject>>(){}.getType();
responseList.addAll(new Gson().fromJson(response.toString(), Type.class));

please check this answer if this can help you, also it will be helpful if you can share the pojo classes. if the issue is because of one to many or many to one relationships this kind of issues can be fixed using @JsonIgnore annotation.

krishna thota
  • 182
  • 1
  • 3
  • 8
  • i don't see any difference between your answer snippet and the one that I've provided here in my question. and the Answer you suggested is again for parsing or deserializing the JSON to java object. I need to understand how to implement nested Java Array listi in recyclerview adapter. – Joe Feb 09 '21 at 18:33