-1

I have been trying to extract array elements from my JSON, but unable to, so far.

I have the following JSON:

{
    "Sample JSON": [{
            "Title": "Title1",
            "Audit": [{
                    "auditId": "01",
                    "type": "sampleType",
                    "auditText": "sampleText",
                    "answer": ["Ans1", "Ans2"]
                },
                {
                    "auditId": "02",
                    "type": "sampleType2",
                    "auditText": "sampleText2",
                    "answer": ["Ans1", "Ans2", "Ans3", "Ans4"]
                }
            ]
        },
        {
            "Title": "Title2",
            "Audit": [{
                    "auditId": "03",
                    "type": "sampleType3",
                    "auditText": "sampleText3",
                    "answer": ["Ans1", "Ans2"]
                },
                {
                    "auditId": "04",
                    "type": "sampleType4",
                    "auditText": "sampleText4",
                    "answer": ["Ans1", "Ans2", "Ans3", "Ans4"]
                }
            ]
        }
    ]
}

I want to extract the array elements of the array 'answer'.

I get exception in the line indicated in comment in the method below:

    public void getAudit(String myJSON) {

        auditList = new ArrayList<AuditList>();
        String title;
        String auditId;
        String type;
        String auditText;
        ArrayList<String> answer = new ArrayList<>();

        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            JSONArray jssArray = jsonObj.getJSONArray("Sample JSON");
            int jsonArrLength = jssArray.length();
            for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);
                title = jsonChildObj.getString("Title");

                JSONArray jssArray1 = jsonChildObj.getJSONArray("Audit");
                for (int i1 = 0; i1 < jssArray1.length(); i1++) {


                    JSONObject jsonChildObj1 = jssArray1.getJSONObject(i1);
                    type = jsonChildObj1.optString("type");
                    auditText = jsonChildObj1.optString("auditText");

                    auditId = jsonChildObj1.optString("auditId");

                    JSONArray jssArray2 = jsonChildObj1.getJSONArray("answer");
                     //Getting exception in above line

                    for (int j=0; j<jssArray2.length(); j++)
                    {
                        answer.add(jssArray2.get(j).toString()) ;
                    }

                    AuditList aList = new AuditList();
                    aList.setQuesId(auditId);
                    aList.setQuesText(auditText);
                    aList.setQuesTypw(auditType);
                    aList.setAnswer(answer);
                    aList.setCategoryName(title);
                    auditList.add(aList);

                }


            }

.
.
.
.
.
}

I found the exception occurring in the line indicated above using the debugger. Couldn't find a way to extract array elements of the array 'answer'. Please help.

2 Answers2

0

The way I do this kind of stuff is by going to this link and then paste my json there and get my class. After that, you can go and add Gson to your gradle by adding this line to your build.gradle:

compile 'com.google.code.gson:gson:2.6.2'

Then just create an instance of the created class that you just created and convert json into your target class:

NewClass classObject = new Gson().fromJson(YOUR_JSON, NewClass.class);

Mohammad Zarei
  • 1,773
  • 14
  • 33
0
if(!jsonChildObj1.get("answer").toString().equals("")){ 
   JSONArray jssArray2 = jsonChildObj1.getJSONArray("answer");
   for (int j=0; j<jssArray2.length(); j++)
   {
      answer.add(jssArray2.get(j).toString()) ;
   }
}

The exception that occurred to you because of Empty Data (""). To Prevent this validate it and getJSONArray(). refer

Mukeshkumar S
  • 785
  • 1
  • 14
  • 30