Java:Json與List對象的相互轉換


谷歌的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);

 

參考來源:https://zhuanlan.zhihu.com/p/44664185

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM