0

I reading this Json , I have created the classes as below , My challange is how to read MultipleChoices to its model class from the response

  [
      {
        "Id": "1",
        "Question": "Suggest areas of improvement in the running of coretec with regards to information technology",
        "HasMultipleChoices": "0",
        "MultipleChoices": []
      },
      {
        "Id": "2",
        "Question": "Did you enjoy last year's team building and what suggestions would you have for tbhis year's teambuilding?",
        "HasMultipleChoices": "0",
        "MultipleChoices": []
      },
      {
        "Id": "3",
        "Question": "Do you think the support you get from management in carrying out your tasks is sufficient?",
        "HasMultipleChoices": "1",
        "MultipleChoices": [
          {
            "Id": "1",
            "MultipleChoice": "Yes"
          },
          {
            "Id": "2",
            "MultipleChoice": "No"
          }
        ]
      }
    ]

This is my mulitple class

public class MultipleChoice {

public String id;
public String multipleChoice;

public MultipleChoice(String id, String multipleChoice) {
    super();
    this.id = id;
    this.multipleChoice = multipleChoice;
}

}

This is my Question Class model

public class Questions {
    public int id;
    public String Questions;
    public String HasMultipleChoices;
    public String MultipleChoices;
    public List<MultipleChoice> multipleChoices = null;

    public Questions(int id, String Questions ,String HasMultipleChoices, List<MultipleChoice> multipleChoices) {
       this.Questions = Questions;
        this.id = id;
        this.HasMultipleChoices = HasMultipleChoices;
        this.multipleChoices = multipleChoices;
    }

}

and this this is how am reading the json through volley

        public void onResponse(String response) {
                    // Do something with the response
                    progressbar.setVisibility(View.GONE);
                    JSONArray jsonarray;
                    List<Questions> list = new ArrayList<>();

                    try {
                        jsonarray  = new JSONArray(response);
                        for (int i=0; i<jsonarray.length(); i++)
                        {
                            //  String ProductName,Price,Category,Description,Brand,Image1,Image2,Image3,Image4,Image5;
                            JSONObject jobObject =jsonarray.getJSONObject(i);
                            Questions item= new Questions(jobObject.getInt("Id"),jobObject.getString("Question")
                                    ,jobObject.getString("HasMultipleChoices"),"WHAT SHOULD I DO HERE TO HAVE A LIST OF MULITPLECHOICES?");

                            list.add(item);
                        }
                        updateList(list);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

6 Answers 6

1

Try the below code:

        public void onResponse(String response) {
                // Do something with the response
                progressbar.setVisibility(View.GONE);
                JSONArray jsonarray;
                List<Questions> list = new ArrayList<>();

                try {
                    jsonarray  = new JSONArray(response);
                    for (int i=0; i<jsonarray.length(); i++)
                    {
                        //  String ProductName,Price,Category,Description,Brand,Image1,Image2,Image3,Image4,Image5;
                        JSONObject jobObject =jsonarray.getJSONObject(i);
     JSONArray multipleChoice=jobObject.getJSONArray("MultipleChoices");
       List<MultipleChoice> multipleChoiceList=new ArrayList<>();
       if(multipleChoice.length()>0)
     {          
     for (int j=0; j<multipleChoice.length(); j++)
                    {
     JSONObject jsonObject =multipleChoice.getJSONObject(j);
       MultipleChoice mc=new    MultipleChoice(jsonObject.getInt("Id"),jsonObject.getString("MultipleChoice"));
       multipleChoiceList.add(mc);
      }
     }
                        Questions item= new Questions(jobObject.getInt("Id"),jobObject.getString("Question")
                                ,jobObject.getString("HasMultipleChoices"),multipleChoiceList);

                        list.add(item);
                    }
                    updateList(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
Sign up to request clarification or add additional context in comments.

2 Comments

Changed the line JSONObject jsonObject =jsonarray.getJSONObject(j); to JSONObject jsonObject = multipleChoice.getJSONObject(j); and it worked . Thanks
Thanks @WanjalaAlex, by mistake I wrote that.
1

Use JSONArray arr = jobObject.getJSONArray("MultipleChoices"); . Send as parameter and in constructor loop through and convert to needed List.

Comments

0

Use Gson

It automatically handle everything.

You just need to define Object model.

For your problem, you do not need to do anything if you use Gson

Comments

0

jobObject.getJSONArray("MultipleChoice") will return a JSONArray object to you. You have to iterate this to create a List<MultipleChoice>. Something like the sample below.

JSONArray multiChoiceArray = jobObject.getJSONArray("MultipleChoice");
List<MultipleChoice> multipleChoices = new ArrayList<>();
for (int i = 0; i < multiChoiceArray.length(); i++) {
  JSONObject multipleChoiceJSON = (JSONObject) multiChoiceArray.get(i);
  multipleChoices.add(new MultipleChoice(multipleChoiceJSON.getInt("Id"),
          multipleChoiceJSON.getInt("multipleChoice")))
}

Comments

0
 try {
            JSONArray jsonarray = new JSONArray(response);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jobObject = jsonarray.getJSONObject(i);
//                Questions item= new Questions(jobObject.getInt("Id"),jobObject.getString("Question")
//                        ,jobObject.getString("HasMultipleChoices"),"WHAT SHOULD I DO HERE TO HAVE A LIST OF MULITPLECHOICES?");
                JSONArray multipleChoices = jobObject.getJSONArray("MultipleChoices");
                if (multipleChoices != null) {
                    for (int j = 0; j < multipleChoices.length(); j++) {
                        JSONObject multipleChoicesJsonObject = jsonarray.getJSONObject(i);
                        int id = multipleChoicesJsonObject.getInt("id");
                        String MultipleChoice = multipleChoicesJsonObject.getString("MultipleChoice");

                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Comments

0
JSONArray multiplechoice= jobObject.getJSONArray("MultipleChoices");
List<MultipleChoice> listmultiple=new ArrayList<>()
for(int i=0;i<multiplechoice.length();i++)

 {  JsonObject multiplechoiceobj=multiplechoice.getJSONObject(i);
    MultipleChoice mul=new MultipleChoice(multiplechoiceobj.getString("id"),multiplechoiceobj.getString("MultipleChoice"));
    listmultiple.add(mul);
 }

Then add this list to

Questions item= new Questions(jobObject.getInt("Id"),jobObject.getString("Question")
                                ,listmultiple);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.