0

I'm currently working on a test app for android and need to parse the JSON test data.

The JSON looks like this:

{"id":1,"name":"Test Test","questions":[{"id":1,"test":1,"text":"Kannst du diese Frage beantworten?","answers":[{"id":1,"question":1,"text":"Ja, das ist richtig.","correct":1},{"id":2,"question":1,"text":"Diese Antwort ist falsch.","correct":0},{"id":3,"question":1,"text":"Diese hier ist ebenfalls nicht so ganz korrekt.","correct":0}]},{"id":2,"test":1,"text":"Diese Frage hier ist nicht korrekt.","answers":[{"id":4,"question":2,"text":"Ich glaube dir nicht, das hier ist eh richtig.","correct":1},{"id":5,"question":2,"text":"Diese Antwort ist falsch.","correct":0},{"id":6,"question":2,"text":"Diese hier ist ebenfalls nicht so ganz korrekt.","correct":0}]}]}

What I need is to extract the questions and its' child data.

What I tried is:

JSONObject jObject = new JSONObject(result);
String test = jObject.getJSONObject("id").getJSONArray("questions").toString();

And

String myString = jObject .getJSONObject("questions").getJSONObject("answers").getString("text");
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
N1l0c
  • 19
  • 2
  • 1
    What were the results of your tries? How did that not meet the expectation? Why do you think that happened? – Ben Jul 02 '18 at 07:00
  • "id" is a number not an object in your json. – gagan singh Jul 02 '18 at 07:01
  • "questions" is a json array instead of an object as you have mentioned in the second code listing. – gagan singh Jul 02 '18 at 07:02
  • @Ben I log the result `String test = jObject.getJSONObject("questions").getJSONArray("answers").toString(); Log.v("RESPOND:", test); } catch (Exception e) { Log.v("RESPOND", "ERROR");` – N1l0c Jul 02 '18 at 07:02
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Absent Jul 02 '18 at 07:06
  • `JSONArray questions = jObject.getJsonArray("questions"); JSONObject firstQuestion = questions.getJSONObject(0); JSONArray answers = firstQuestion.getJSONArray("answers"); ...`??? – fabian Jul 02 '18 at 07:24

3 Answers3

0

Parse your data like this :

public class DataModel {
    int id;
    String name;
    ArrayList<Question> questions = new ArrayList<>();


    public class Question {
        int id;
        int test;
        String text;
        ArrayList<Answers> answers = new ArrayList<>();
    }

    public class Answers {
        int id;
        int question;
        String text;
        int correct;
    }


    public DataModel parseResponce(JSONObject response) {
        id = response.optInt("id");
        name = response.optString("name");
        JSONArray array = response.optJSONArray("questions");
        parseQuestion(array);
        return this;
    }


    public void parseQuestion(JSONArray questions) {
        for (int i = 0; i < questions.length(); i++) {
            JSONObject object = questions.optJSONObject(i);
            Question question = new Question();
            question.id = object.optInt("id");
            question.test = object.optInt("test");
            question.text = object.optString("text");
            JSONArray array = object.optJSONArray("answers");
            for (int j = 0; j < array.length(); j++) {
                JSONObject answer = questions.optJSONObject(i);
                Answers ans = new Answers();
                ans.id = answer.optInt("id");
                ans.question = answer.optInt("question");
                ans.text = answer.optString("text");
                ans.correct = answer.optInt("correct");
                question.answers.add(ans);
            }
            this.questions.add(question);
        }
    }
}

Just call the parseResponce(JSONObject response) method and pass you response you will get question and answers inside the DataModel object;

Shiv Jalkote
  • 111
  • 6
0
 try {
                            JSONObject data = new JSONObject(result);
                            int id = data.getInt("id");
                            String name=data.getString("name");
                            JSONArray list_question=data.getJSONArray("questions");
                            int length=list_question.length();
                            for (int i=0;i<length;i++){
                                JSONObject question=list_question.getJSONObject(i);
                                int question_id=question.getInt("id");
                                int test=question.getInt("test");
                                String question_text=question.getString("text");
                                JSONArray list_answer=question.getJSONArray("answers");
                                int lenght_answer=list_answer.length();
                                for (int j=0;i<lenght_answer;i++){
                                    JSONObject answer=list_answer.getJSONObject(j);

                                    int answer_id=answer.getInt("id");
                                    int que=answer.getInt("question");
                                    String answer_text=answer.getString("text");
                                    int status=answer.getInt("correct");



                                }

                            }
                        }catch (JSONException ex){}
Dev
  • 181
  • 12
0

Try this

        try
        {
            String id = response.getString("id");
            String name = response.getString("name");
            JSONArray questionsArray = response.getJSONArray("questions");
            for(int i = 0; i <questionsArray.length(); i++)
            {
                JSONObject jsonObject = questionsArray.getJSONObject(i);
                String sid = jsonObject.getString("id");
                String stest = jsonObject.getString("test");
                String stext = jsonObject.getString("text");
                JSONArray answersArray = jsonObject.getJSONArray("answers");
                for(int j=0; j<answersArray.length(); j++)
                {
                    JSONObject jsonObjectAns = answersArray.getJSONObject(j);
                    String sidAns = jsonObject.getString("id");
                    String stextquestion = jsonObject.getString("question");
                    String stextAns = jsonObject.getString("text");
                    String scorrectAns = jsonObject.getString("correct");
                }
            }

        } catch (JSONException e)
        {
            e.printStackTrace();
        }
Himanshu itmca
  • 395
  • 5
  • 22