【问题标题】:How to get List array inside multiple list array in android using pojo class如何使用pojo类在android中的多个列表数组中获取列表数组
【发布时间】:2018-01-17 06:58:28
【问题描述】:

我收到了来自 api 的以下响应 我必须得到这样的回应:

@SerializedName("AllQuestions")
public List<AllQuestions> AllQuestions;

我有 AllQuestion 数组,但现在如何进入 pojo 类中的多个列表数组

"AllQuestions": [
                                        [
                                            {
                                                "QuestionID": "1",
                                                    "QuestionText": "What was the name of your elementary/primary school?"
                                            },
                                            {
                                                "QuestionID": "2",
                                                    "QuestionText": "What was your childhood nickname?"
                                            }
                                        ],
                                        [
                                            {
                                                "QuestionID": "11",
                                                    "QuestionText": "What was your favorite sport in high school?"
                                            },
                                            {
                                                "QuestionID": "12",
                                                    "QuestionText": "What was the name of the hospital where you  born?"
                                            }
                                        ],
                                        [
                                            {
                                                "QuestionID": "21",
                                                    "QuestionText": "Where were you when you had your first kiss?"
                                            },
                                            {
                                                "QuestionID": "22",
                                                    "QuestionText": "What are the last 4 digits of your driving license?"
                                            },
                                        ]
                                    ]

AllQuestion.java(POJO 类)

@SerializedName("QuestionID")
public String QuestionID;

@SerializedName("QuestionText")
public String QuestionText;

【问题讨论】:

    标签: android arraylist retrofit2 pojo


    【解决方案1】:

    第一步:访问http://www.jsonschema2pojo.org/

    第 2 步:复制您的响应并将其粘贴到那里并输入包和类名

    第 3 步:选择目标语言为 Java

    第 4 步:将源类型标记为 Json

    第 5 步:将注释样式标记为 Gson

    第 6 步:预览

    第 7 步:将这些类复制到您的应用程序包中

    【讨论】:

    • 是的,这是一种很好的方法,但是对于 null 值,它只会创建 Object 类实例,请确保手动修改它们。
    【解决方案2】:

    Question.java

    public class Question {
    
    @SerializedName("QuestionID")
    public String QuestionID;
    
    @SerializedName("QuestionText")
    public String QuestionText;
    }
    

    AllQuestion.java

    public class AllQuestion{
      public List<Question> questions; // no need of serialization as inner array 
        doesn't have any keys
    }
    

    然后在下面使用

    @SerializedName("AllQuestions")
    public List<AllQuestions> AllQuestions;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 2019-11-02
      • 2020-11-28
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多