【问题标题】:how to save Complex json array and jsonobject in model class如何在模型类中保存复杂的json数组和jsonobject
【发布时间】:2018-11-13 17:38:40
【问题描述】:

如何获取其他jsonarray中的json数组。

{
   "Detail":{
      "FirstName":"Raj",
      "LastName":"Yadav",
      "ConstQuestions":[
         {
            "QuestionSetId":247,
            "PathwayId":177,
            "UserId":0,
            "RuleId":0,
            "Questions":[
               {
                  "QuestionId":348,
                  "QuestionName":"Do you have excessive daytime sleepiness",
                  "choicelist":[
                     {
                        "choiceId":784,
                        "ChoiceName":"Yes"
                     },
                     {
                        "choiceId":785,
                        "ChoiceName":"No"
                     }
                  ]
               }
            ]
         }
      ]
   }
}

我上面提到了 JSON 响应的格式。我无法获取choiceList数组

【问题讨论】:

  • 这是一个无效的 json 请分享正确的回复
  • 通过索引从 JSONArray 中获取 JSONObject,然后通过名称从 JSONObject 中获取 JSONArray。尽可能多地执行此操作以访问choicelist

标签: android


【解决方案1】:

您可以通过以下代码实现列表。

 String s="Your JSON String";
    try {
        JSONObject main=new JSONObject(s);
        JSONObject detail=main.getJSONObject("Detail");
        JSONArray ConstQuestions=detail.getJSONArray("ConstQuestions");
        for(int l=0;l<ConstQuestions.length();l++){
            JSONObject OBJ=ConstQuestions.getJSONObject(l);
            JSONArray choicelistArray=OBJ.getJSONArray("choicelist");
              for (int k = 0; k < choicelistArray.length(); k++) {
                        JSONObject object3 =choicelistArray.getJSONObject(k);
                        String choiceId = object3.getString("choiceId");
                        String ChoiceName = object3.getString("ChoiceName");
              }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

这里的choicelistArray是你的选择列表数组

【讨论】:

    【解决方案2】:
    Use below model class using that you achieve what you want:
    
    
    
    import java.io.Serializable;
    import java.util.List;
    
    public class ComplexModel implements Serializable{
    
        @SerializedName("Detail")
        @Expose
        private Detail detail;
    
        public Detail getDetail() {
            return detail;
        }
    
        public void setDetail(Detail detail) {
            this.detail = detail;
        }
    
        public class Detail implements Serializable{
    
            @SerializedName("FirstName")
            @Expose
            private String firstName;
            @SerializedName("LastName")
            @Expose
            private String lastName;
            @SerializedName("ConstQuestions")
            @Expose
            private List<ConstQuestion> constQuestions = null;
    
            public String getFirstName() {
                return firstName;
            }
    
            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }
    
            public String getLastName() {
                return lastName;
            }
    
            public void setLastName(String lastName) {
                this.lastName = lastName;
            }
    
            public List<ConstQuestion> getConstQuestions() {
                return constQuestions;
            }
    
            public void setConstQuestions(List<ConstQuestion> constQuestions) {
                this.constQuestions = constQuestions;
            }
    
            public class ConstQuestion implements Serializable{
    
                @SerializedName("QuestionSetId")
                @Expose
                private Integer questionSetId;
                @SerializedName("PathwayId")
                @Expose
                private Integer pathwayId;
                @SerializedName("UserId")
                @Expose
                private Integer userId;
                @SerializedName("RuleId")
                @Expose
                private Integer ruleId;
                @SerializedName("Questions")
                @Expose
                private List<Question> questions = null;
    
                public Integer getQuestionSetId() {
                    return questionSetId;
                }
    
                public void setQuestionSetId(Integer questionSetId) {
                    this.questionSetId = questionSetId;
                }
    
                public Integer getPathwayId() {
                    return pathwayId;
                }
    
                public void setPathwayId(Integer pathwayId) {
                    this.pathwayId = pathwayId;
                }
    
                public Integer getUserId() {
                    return userId;
                }
    
                public void setUserId(Integer userId) {
                    this.userId = userId;
                }
    
                public Integer getRuleId() {
                    return ruleId;
                }
    
                public void setRuleId(Integer ruleId) {
                    this.ruleId = ruleId;
                }
    
                public List<Question> getQuestions() {
                    return questions;
                }
    
                public void setQuestions(List<Question> questions) {
                    this.questions = questions;
                }
    
                public class Question implements Serializable{
    
                    @SerializedName("QuestionId")
                    @Expose
                    private Integer questionId;
                    @SerializedName("QuestionName")
                    @Expose
                    private String questionName;
                    @SerializedName("choicelist")
                    @Expose
                    private List<Choicelist> choicelist = null;
    
                    public Integer getQuestionId() {
                        return questionId;
                    }
    
                    public void setQuestionId(Integer questionId) {
                        this.questionId = questionId;
                    }
    
                    public String getQuestionName() {
                        return questionName;
                    }
    
                    public void setQuestionName(String questionName) {
                        this.questionName = questionName;
                    }
    
                    public List<Choicelist> getChoicelist() {
                        return choicelist;
                    }
    
                    public void setChoicelist(List<Choicelist> choicelist) {
                        this.choicelist = choicelist;
                    }
    
                }
    
                public class Choicelist implements Serializable{
    
                    @SerializedName("choiceId")
                    @Expose
                    private Integer choiceId;
                    @SerializedName("ChoiceName")
                    @Expose
                    private String choiceName;
    
                    public Integer getChoiceId() {
                        return choiceId;
                    }
    
                    public void setChoiceId(Integer choiceId) {
                        this.choiceId = choiceId;
                    }
    
                    public String getChoiceName() {
                        return choiceName;
                    }
    
                    public void setChoiceName(String choiceName) {
                        this.choiceName = choiceName;
                    }
    
                }
    
            }
        }
    }
    

    然后检查:

    ComplexModel.Detail complexModel = new ComplexModel().new Detail();
        ArrayList<ComplexModel.Detail.ConstQuestion.Choicelist> choicelists = new ArrayList<>();
    
        if (complexModel.getConstQuestions() != null) {
            if (complexModel.getConstQuestions().get(0).getQuestions() != null) {
                for (int i=0;i<complexModel.getConstQuestions().get(0).getQuestions().size();i++) {
                    if (complexModel.getConstQuestions().get(0).getQuestions().get(i).getChoicelist() != null) {
                        choicelists.addAll(complexModel.getConstQuestions().get(0).getQuestions().get(i).getChoicelist());
                    }
                }
            }
        }
    

    使用这个你很容易实现你想要的。

    【讨论】:

    • SerializedName 和 Expose 都 r 为红色
    • 我已经解决了这个错误问题。现在请让我知道如何在其中添加我的 json 数据列表?
    • 现在,当服务器调用完成时,将整个数据放入我在上面定义的模型中
    • 将响应存储到 complexModel 此类模型中,然后放入 if 条件我在回答中提到
    【解决方案3】:

    这是您的解决方案。您已解析 JSON 响应,如下所示。

    更新答案

    首先在onCreate方法上面创建三个列表

    List<choicelist> choicelist;
    List<Questions> Questions;
    List<ConstQuestions> ConstQuestions;
    

    现在在 onCreate 方法中

    ConstQuestions = new ArrayList();
    

    现在转到解析数据并编写此函数的函数

    choicelist = new ArrayList();
    Questions = new ArrayList();
    

    代码已更新

    JSONObject object = new JSONObject("<YOUR STRING>");
                    JSONObject detailObject = object.getJSONObject("Detail");
                    String firstName = detailObject.getString("FirstName");
                    String lasttName = detailObject.getString("LastName");
    
                    JSONArray ConstQuestionsArray = detailObject.getJSONArray("ConstQuestions");
                    for (int i = 0; i < ConstQuestionsArray.length(); i++) {
                        JSONObject object1 = ConstQuestionsArray.getJSONObject(i);
                        String QuestionSetId = object1.getString("QuestionSetId");
                        String PathwayId = object1.getString("PathwayId");
                        String UserId = object1.getString("UserId");
                        String RuleId = object1.getString("RuleId");
    
                        JSONArray QuestionsArray = object1.getJSONArray("Questions");
                        for (int j = 0; j < QuestionsArray.length(); j++) {
                            JSONObject object2 = QuestionsArray.getJSONObject(j);
                            String QuestionId = object2.getString("QuestionId");
                            String QuestionName = object2.getString("QuestionName");
    
                            JSONArray choicelistArray = object2.getJSONArray("choicelist");
                            for (int k = 0; k > choicelistArray.length(); k++) {
                                JSONObject object3 = choicelistArray.getJSONObject(k);
                                String choiceId = object3.getString("choiceId");
                                String ChoiceName = object3.getString("ChoiceName");
                                /*UPDATED CODE*/
                                ConstQuestions.choicelist choicelists = new ConstQuestions.choicelist(choiceId, ChoiceName);
                                choicelist.add(choicelists);
                            }
                            /*UPDATED CODE*/
                            ConstQuestions.Questions questi = new ConstQuestions.Questions(QuestionId, QuestionName, choicelist);
                            Questions.add(questi);
                        }
                        /*UPDATED CODE*/
                        ConstQuestions ConstQuestionss = new ConstQuestions(QuestionSetId, PathwayId, UserId, RuleId, Questions);
                    }
    

    试试这个,如果有帮助,请接受答案。

    【讨论】:

    • 谢谢,:)。我可以知道如何制作它的模型类吗?
    • 对于所有参数还是只选择一个?
    • "ConstQuestions":[ { "QuestionSetId":247, "PathwayId":177, "UserId":0, "RuleId":0, "Questions":[ { "QuestionId":348, "QuestionName":"你白天嗜睡过度吗", "choicelist":[ { "choiceId":784, "ChoiceName":"Yes" }, ]}]}]
    • 对于这些我想做模型类来设置值
    • 无法在模型类@Shane sir 中保存列表数据
    猜你喜欢
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2017-04-25
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多