Navamani Samuel Navamani Samuel - 1 month ago 8
Java Question

How to Create JSONArray for a List<Class name>

I have a class called

class Student {
String name;
String age;
}


I have a method that returns List object like

public List<Student> getList(){

List<Student> li =new ArrayList();
....

li.add(new Student('aaa','12'));
...

return li;
}


I need to convert that list into JSONArray like this

[{"name":"sam","age":"12"},{"name":"sri","age":"5"}]


Can anyone help me to get this? Thanks in Advancee..

Answer

I think you need not download the jettison jar file.

Using JSONArray and JSONObject you can easily convert that list into JSON object like @Juniad answer

Comments