Skip to content
Advertisement

How to put Json object through loop

I want to loop through the questionnaires by using the for loop to set the value of the EMAIL_TO_CLIENT_IND.

The value inside the “path” is QUESTIONNAIRES/SUB_QUESTION/EMAIL_TO_CLIENT_IND. The first “if” is if the level path in JSON is just one Ex. “Questionnaires”. And the “else” is for the path more than 1 Ex. “Questionnaires/Question”.

My current idea is put the jObject.getJSONObject() inside the “else” for loop.

enter image description here

enter image description here

Advertisement

Answer

You can use the JSONArray as follows

String json="{"QUESTIONNAIRES":[{n" +
                "  "SUB_QUESTION":[{n" +
                "    "EMAIL_TO_CLIENT_IND":"N"n" +
                "  }]n" +
                "}]}";

try {
   JSONObject mainObject=new JSONObject(json);
   JSONArray questionnairesArray=mainObject.getJSONArray("QUESTIONNAIRES");
   for (int i=0;i<questionnairesArray.length();i++){
      JSONObject questionnairesObject= (JSONObject) questionnairesArray.get(i);
      JSONArray subQuestionArray=questionnairesObject.getJSONArray("SUB_QUESTION");
      for (int j=0;j<subQuestionArray.length();j++){
        JSONObject subQuestionObject= (JSONObject) subQuestionArray.get(i);
           subQuestionObject.put("EMAIL_TO_CLIENT_IND","Y");
      }
   }
   Log.e("Output",mainObject.toString());
 } catch (JSONException e) {
   Log.e("Exception",e.getMessage());
 }

Edited

can be solved using GSON library also

//frame data classes
class Response {
        @SerializedName("QUESTIONNAIRES")
        private List<Questionaire> questionaireList;

        public List<Questionaire> getQuestionaireList() {
            return questionaireList;
        }

        public void setQuestionaireList(List<Questionaire> questionaireList) {
            this.questionaireList = questionaireList;
        }
    }

    class Questionaire {
        @SerializedName("SUB_QUESTION")
        private List<SubQuestion> subQuestionList;

        public List<SubQuestion> getSubQuestionList() {
            return subQuestionList;
        }

        public void setSubQuestionList(List<SubQuestion> subQuestionList) {
            this.subQuestionList = subQuestionList;
        }
    }

    class SubQuestion {
        @SerializedName("EMAIL_TO_CLIENT_IND")
        private String emailToClientId;

        public String getEmailToClientId() {
            return emailToClientId;
        }

        public void setEmailToClientId(String emailToClientId) {
            this.emailToClientId = emailToClientId;
        }
    }
     
    //loop through data using GSON
    Gson gson = new Gson();
    String json = "{"QUESTIONNAIRES":[{n" +
                "  "SUB_QUESTION":[{n" +
                "    "EMAIL_TO_CLIENT_IND":"N"n" +
                "  }]n" +
                "}]}";
        Response response = gson.fromJson(json, Response.class);
        for (Questionaire questionnaire : response.getQuestionaireList()) {
            for (SubQuestion subQuestion : questionnaire.getSubQuestionList()) {
                subQuestion.setEmailToClientId("Y");
            }
        }
        Log.e("Output", gson.toJson(response));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement