如何为List< Class name>创建JSONArray;

如何为List< Class name>创建JSONArray;

问题描述:

我有一堂课

class Student {
   String name;
   String age;
}

我有一个返回

public List<Student> getList(){

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

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

 return li;    
}

我需要像这样将列表转换成JSONArray

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..

我认为您无需下载Jettison jar文件.

I think you need not download the jettison jar file.

使用JSONArrayJSONObject,您可以轻松地将该列表转换为JSON对象,例如@Juniad答案

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