如何为列表创建 JSONArray<类名> [英] How to Create JSONArray for a List&lt;Class name&gt;

查看:30
本文介绍了如何为列表创建 JSONArray<类名>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级

class Student {
   String name;
   String age;
}

我有一个返回 List 对象的方法,如

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

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

I need to convert that list into JSONArray like this

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

谁能帮我弄到这个?

推荐答案

我认为你不需要下载抛弃 jar 文件.

I think you need not download the jettison jar file.

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

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

这篇关于如何为列表创建 JSONArray<类名>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆