Java中Json转List方法
Java利用Json-lib包进行json字符串转换成List集合
JSONArray转换List用法实例
public void JSON2List() { try { fail("==============JSON Arry String >>> Java List =================="); String json = "{\"address\":\"chian\",\"birthday\":{\"birthday\":\"2010-11-22\"}," + "\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"}"; json = "[" + json + "]"; jsonArray = JSONArray.fromObject(json); List < Student > list = JSONArray.toList(jsonArray, Student.class); System.out.println(list.size()); System.out.println(list.get(0)); list = JSONArray.toList(jsonArray); System.out.println(list.size()); System.out.println(list.get(0)); //MorphDynaBean } catch(Exception e) { e.printStackTrace(); } }
执行结果:
==============JSON Arry String >>> Java List ================== 1 tom#22#chian#2010-11-22#email@123.com 1 net.sf.ezmorph.bean.MorphDynaBean@141b571[{id=22, birthday=net.sf.ezmorph.bean.MorphDynaBean@b23210[{birthday=2010-11-22}], address=chian, email=email@123.com, name=tom}]
JSONSerializer转换List方法实例
String json = "[\"first\",\"second\"]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); List output = (List) JSONSerializer.toJava(jsonArray);
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。