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:

//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);  

原文地址:https://blog.csdn.net/demonliuhui/article/details/52949079

猜你喜欢

转载自blog.csdn.net/JoneZ/article/details/82699446