我读了这个Json,我创建了如下的类,我的挑战是如何从响应中读取MultipleChoices到它的模型类
[
{
"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"
}
]
}
]
这是我的多重课程
public class MultipleChoice {
public String id;
public String multipleChoice;
public MultipleChoice(String id, String multipleChoice) {
super();
this.id = id;
this.multipleChoice = multipleChoice;
}
}
这是我的问题类型号
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;
}
}
这就是我通过排球阅读json的方式
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();
}
}
答案 0 :(得分:1)
使用TREE
。作为参数发送并在构造函数中循环并转换为所需的List。
答案 1 :(得分:1)
请尝试以下代码:
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();
}
}
答案 2 :(得分:0)
答案 3 :(得分:0)
jobObject.getJSONArray("MultipleChoice")
会向您返回JSONArray
个对象。您必须重复此操作才能创建List<MultipleChoice>
。类似下面的示例。
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")))
}
答案 4 :(得分: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();
}
答案 5 :(得分: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);
}
然后将此列表添加到
Questions item= new Questions(jobObject.getInt("Id"),jobObject.getString("Question")
,listmultiple);