List转Json数组
生活随笔
收集整理的這篇文章主要介紹了
List转Json数组
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
海豚精靈:https://www.whhtjl.com;優(yōu)課GO:https://mgo.whhtjl.com
java--List轉(zhuǎn)換成json格式
方法一
首先導(dǎo)入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轉(zhuǎn)換成json很像是java對map的操作。
方法二
第二種方法更加簡單,沒有類似map操作的步驟,只需要引入相關(guān)jar包,就可以調(diào)用已有的函數(shù)fromObject(),其參數(shù)輸入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;}}注意這個實(shí)例導(dǎo)入的JSONArray是net.sf.json.JSONArray,上邊的導(dǎo)入的是org.json.JSONArray。
?
總結(jié)
以上是生活随笔為你收集整理的List转Json数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: caffe caffe.cpp 程序入口
- 下一篇: 链表创建、逆置、删除详解