回收者视图中的数据解析

时间:2018-12-13 07:17:43

标签: android json parsing android-recyclerview

"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
    },

] }

从此JSON响应中,我想分别命名1个部分和下一个部分,命名各部分,并在android的回收器视图中设置

例如:

名称1 --- A,B,C 名称2-A,B 名称3-A,B,C,D

1 个答案:

答案 0 :(得分:0)

 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;
    }
}