Android 回收器视图中的数据解析
] } 从这个JSON响应中,我想分别命名1个部分,然后在android的recycler视图中分别命名和设置部分 例如: 名称1---A、B、C 名字2——A,B 名称3——A、B、C、DAndroid 回收器视图中的数据解析,android,json,parsing,android-recyclerview,Android,Json,Parsing,Android Recyclerview,] } 从这个JSON响应中,我想分别命名1个部分,然后在android的recycler视图中分别命名和设置部分 例如: 名称1---A、B、C 名字2——A,B 名称3——A、B、C、Dpublic void parseJson(字符串jsonString){ 字符串strJson=jsonString; 试一试{ JSONArray数据=新JSONArray(strJson); ArrayList studentList=新建ArrayList(); 对于(int i=0;i
public void parseJson(字符串jsonString){
字符串strJson=jsonString;
试一试{
JSONArray数据=新JSONArray(strJson);
ArrayList studentList=新建ArrayList();
对于(int i=0;i
你能解释一下你想做得更好一点吗?我不明白你所说的话的意思。遵循以下链接:首先,你可以将json响应转换为POJO,变成这样,然后你继续为你的recycleview、handler创建一个适配器,并根据@Ali的链接完成所有操作。可能会重复此链接。
"data": [
{
"Id": 1,
"Schoolid": 0,
"Name": "1",
"Section": "A",
"CreatedOn": null
},
{
"Id": 2,
"Schoolid": 0,
"Name": "1",
"Section": "B",
"CreatedOn": null
},
{
"Id": 3,
"Schoolid": 0,
"Name": "1",
"Section": "C",
"CreatedOn": null
},
{
"Id": 4,
"Schoolid": 0,
"Name": "2",
"Section": "A",
"CreatedOn": null
},
{
"Id": 5,
"Schoolid": 0,
"Name": "2",
"Section": "B",
"CreatedOn": null
},
public void parseJson(String jsonString) {
String strJson = jsonString;
try {
JSONArray data = new JSONArray(strJson);
ArrayList<Student> studentList = new ArrayList<>();
for (int i = 0; i < data.length(); i++) {
Student student = new Student();
student.setId(data.getJSONObject(i).getInt("Id"));
student.setName(data.getJSONObject(i).getString("Name"));
student.setSection(data.getJSONObject(i).getString("Section"));
student.setCreatedOn(data.getJSONObject(i).getString("CreatedOn"));
studentList.add(student);
}
// Here you can use student list
Log.d("Students", studentList.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
// This is pojo class of students which can be used to store student object
public class Student {
int id;
String name;
String section;
String createdOn;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getCreatedOn() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn = createdOn;
}
}