How to Create JSONArray for a List

后端 未结 7 2007
南旧
南旧 2020-12-30 00:47

I have a class called

class Student {
   String name;
   String age;
}

I have a method that returns List object like

publi         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 01:29

    You will have to include the jettison jar in you project and import the required classes.

    JSONObject jObject = new JSONObject();
    try
    {
        JSONArray jArray = new JSONArray();
        for (Student student : sudentList)
        {
             JSONObject studentJSON = new JSONObject();
             studentJSON.put("name", student.getName());
             studentJSON.put("age", student.getAge());
             jArray.put(studentJSON);
        }
        jObject.put("StudentList", jArray);
    } catch (JSONException jse) {
        jse.printStacktrace();
    }
    

提交回复
热议问题