Json与List,Map的相互转换

谷歌的Gson.jar:

//list转换为json
Gson gson = new Gson();  
List<Person> persons = new ArrayList<Person>();  
String str = gson.toJson(persons);  
  • 1
  • 2
  • 3
  • 4
//json转换为list
Gson gson = new Gson();  
List<Person> persons = gson.fromJson(str, new TypeToken<List<Person>>(){}.getType());  
  • 1
  • 2
  • 3

阿里的fastJson.jar:

//list转换为json
List<CustPhone> list = new ArrayList<CustPhone>();
String str=JSON.toJSON(list).toString();
  • 1
  • 2
  • 3
//json转换为list
  List<Person> list = new ArrayList<Person>();  
        list = JSONObject.parseArray(jasonArray, Person.class);  
  • 1
  • 2
  • 3

导入jar包直接调用,简单无烦恼。Map相同

参考: 
http://blog.csdn.net/qxs965266509/article/details/42774691 
http://huyizizhen.iteye.com/blog/1453621 
http://blog.csdn.net/u010797575/article/details/43304981 
http://blog.csdn.net/paincupid/article/details/51221391

猜你喜欢

转载自blog.csdn.net/carrybest/article/details/79869717
今日推荐