Java中Json和List转换

一、依赖Gson:

//list转换为json
Gson gson = new Gson();  
List users = new ArrayList();  
String str = gson.toJson(users);  

//json转换为list
Gson gson = new Gson();  
List users = gson.fromJson(str, new TypeToken>(){}.getType());  


二、依赖fastJson:

//list转换为json
List list = new ArrayList();
String str=JSON.toJSON(list).toString();

//json转换为list
List list = new ArrayList();  
list = JSONObject.parseArray(jasonArray, User.class);  

 

你可能感兴趣的:(java)