明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

Java:Json与List对象的相互转换

Posted on 2022-09-20 08:28  且行且思  阅读(2854)  评论(0编辑  收藏  举报

谷歌的Gson.jar:

//list转换为json

Gson gson = new Gson();

List<Person> persons = new ArrayList<Person>();

String str = gson.toJson(persons);

//json转换为list

Gson gson = new Gson();

List<Person> persons = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());

 

阿里的fastJson.jar(import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;)

//list转换为json

List<CustPhone> list = new ArrayList<CustPhone>();

String str=JSON.toJSON(list).toString();

//json转换为list

List<Person> list = new ArrayList<Person>();
list = JSONObject.parseArray(jasonArray, Person.class);

 

实用举例:

参数:JSONObject params

List<DlvOrderDto> dlvOrderDtos = new ArrayList<>();
String str = JSON.toJSON(params.get("rowData")).toString();
dlvOrderDtos = JSONObject.parseArray(str, DlvOrderDto.class);