海豚精灵https://www.whhtjl.com优课GOhttps://mgo.whhtjl.com

java--List转换成json格式

方法一

首先导入jar包,json-rpc-1.0.jar

public class List2Json {

public static JSONArray ProLogList2Json(List<ProgramLog> list){

JSONArray json = new JSONArray();

for(ProgramLog pLog : list){

JSONObject jo = new JSONObject();

jo.put("id", pLog.getId());

jo.put("time", pLog.getBeginTime());

json.put(jo);

}

return json;

}

list转换成json很像是java对map的操作。

方法二

第二种方法更加简单,没有类似map操作的步骤,只需要引入相关jar包,就可以调用已有的函数fromObject(),其参数输入list,其返回值就是json。jar包如下:

commons-beanutils-1.7.jarcommons-collections.jarcommons-lang.jarezmorph.jarjson-lib-2.2.2-jdk15.jar

import java.util.List;

import net.sf.json.JSONArray;

import com.test.vo.ProgramLog;

public class List2Json1 {

public static JSONArray List2Json(List<ProgramLog> list){

JSONArray json = JSONArray.fromObject(list);

return json;

}

}

注意这个实例导入的JSONArray是net.sf.json.JSONArray,上边的导入的是org.json.JSONArray。

 

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 2 个月前
8c391e04 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐