解析嵌套的JSONArray并以Recyclerview正确的方式显示 [英] Parse Nested JSONArray and show it in Recyclerview Proper way

查看:51
本文介绍了解析嵌套的JSONArray并以Recyclerview正确的方式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Gson解析嵌套的JSON数组,并在recyclerview中显示该数组.我解析JSON数组的方式对我来说似乎不正确/正确,但是可以完成工作.但是我想知道如何正确处理这种情况.

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.

示例JSONArray

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

使用Gson进行解析(仅关注Response数组):

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

直到这里,我都在一个主列表中解析完整的 ResponseArray .之后,我用Getter/Setter创建了一个新的不同对象类型的列表,并通过 responseList 进行了迭代,如果 Content.size()是大于0,我正在执行以下操作.

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

现在这是一个带有嵌套对象的简单数组,我可以在 recycerlview适配器中通过此数组进行简单迭代,而不会出现任何问题,并使用基于的不同布局配置空值.

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.

SO上的许多链接基本上都在解析嵌套数组,我可以轻松地做到这一点,但是没有人在谈论如何在Recyclerview Adapter中使用解析后的嵌套数组.

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.

推荐答案

您可以尝试以这种方式使用

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

如果可以,请检查此 answer 可以帮助您,如果您可以分享pojo课程,也会有所帮助.如果问题是由于一对多或多对一的关系引起的,则可以使用 @JsonIgnore 批注解决此类问题.

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.

这篇关于解析嵌套的JSONArray并以Recyclerview正确的方式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆