Json与List的相互转换 [谷歌的Gson.jar和阿里的fastJson.jar]

导入jar包直接调用,简单粗暴无烦恼!

jar文件:https://files.cnblogs.com/files/huanglibin/%E6%AD%8C%E7%9A%84Gson.jar%E5%92%8C%E9%98%BF%E9%87%8C%E7%9A%84fastJson.jar.rar



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

 

posted @ 2017-06-27 09:29  hlb  阅读(732)  评论(0编辑  收藏  举报