在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML、JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好,下面先看下JSON的格式,
JSON可以有两种格式,一种是对象格式的,另一种是数组对象。
要想实现JSON和java对象之间的互转,需要借助第三方jar包,这里使用json-lib这个jar包,下载地址为:https://sourceforge.net/projects/json-lib/,json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五个包的支持,可以自行从网上下载,这里不再贴出下载地址。
json-lib提供了几个类可以完成此功能,例,JSONObject、JSONArray。从类的名字上可以看出JSONObject转化的应该是对象格式的,而JSONArray转化的则应该是数组对象
一、java普通对象和json字符串的互转
//将Java对象转换为Json字符串 public static void convertObject() { Student stu=new Student(); stu.setName("张三"); stu.setAge("23"); stu.setAddress("上海浦东新区"); Student stu2=new Student(); stu2.setName("李四"); stu2.setAge("25"); stu2.setAddress("安徽省合肥市"); //1、使用JSONObject JSONObject json = JSONObject.fromObject(stu); String strJson=json.toString(); System.out.println("strJson:"+strJson); System.out.println("--------------------"); //2、使用JSONArray JSONArray array=JSONArray.fromObject(stu);//第一个对象 array.add(stu2);//第二个对象 String strArray=array.toString(); System.out.println("strArray:"+strArray); } //将JSon字符串转换为Java对象 public static void jsonStrToJava(){ //定义两种不同格式的字符串 String objectStr="{\"name\":\"张三\",\"age\":\"23\",\"address\":\"上海浦东新区\"}"; String arrayStr="[{\"name\":\"张三\",\"age\":\"23\",\"address\":\"上海浦东新区\"},{\"name\":\"李四\",\"age\":\"25\",\"address\":\"安徽省合肥市\"}]"; //1、使用JSONObject JSONObject jsonObject=JSONObject.fromObject(objectStr); Student stu=(Student)JSONObject.toBean(jsonObject, Student.class); System.out.println("stu:"+stu); System.out.println("--------------------------"); //2、使用JSONArray JSONArray jsonArray=JSONArray.fromObject(arrayStr); for(int i=0;i<jsonArray.size();i++){ //获得jsonArray的每个元素 Object o=jsonArray.get(i); JSONObject jsonObject2=JSONObject.fromObject(o); Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class); System.out.println("stu2:"+stu2); } }
二、list和json字符串的互转
三、map和json字符串的互转//将List转换为JSon字符串 public static void listToJSON(){ Student stu=new Student(); stu.setName("张三"); stu.setAge("23"); stu.setAddress("上海浦东新区"); List<Student> lists=new ArrayList<Student>(); lists.add(stu); //1、使用JSONObject //JSONObject listObject=JSONObject.fromObject(lists); //System.out.println("listObject:"+listObject.toString()); //2、使用JSONArray JSONArray listArray=JSONArray.fromObject(lists); System.out.println("listArray:"+listArray.toString()); } //将json转换为list @SuppressWarnings("unchecked") public static void jsonToList(){ String arrayStr="[{\"name\":\"张三\",\"age\":\"23\",\"address\":\"上海浦东新区\"},{\"name\":\"李四\",\"age\":\"25\",\"address\":\"安徽省合肥市\"}]"; //转化为list List<Student> list2= (List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), new Student(),new JsonConfig()); for (Student stu : list2) { System.out.println(stu); } System.out.println("------------------------"); //转化为数组 Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class); for (Student student : ss) { System.out.println(student); } }
//将Map转换为JSon字符串 public static void mapToJSON(){ Student stu=new Student(); stu.setName("张三"); stu.setAge("23"); stu.setAddress("上海浦东新区"); Map<String,Student> map=new HashMap<String,Student>(); map.put("first", stu); //1、JSONObject JSONObject mapObject=JSONObject.fromObject(map); System.out.println("mapObject"+mapObject.toString()); //2、JSONArray JSONArray mapArray=JSONArray.fromObject(map); System.out.println("mapArray:"+mapArray.toString()); } //将JSon字符串转换为Map public static void jsonToMap(){ String strObject="{\"first\":{\"address\":\"中国上海\",\"age\":\"23\",\"name\":\"JSON\"}}"; //JSONObject JSONObject jsonObject=JSONObject.fromObject(strObject); Map map=new HashMap(); map.put("first", Student.class); //使用了toBean方法,需要三个参数 MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map); System.out.println(my.getFirst()); }
第一次写博客,代码是自己敲的,不过是参照http://www.cnblogs.com/teach/p/5791029.html#3715382 这位兄台的。
源码如下: