jsonGenerator"aGVsbG8gd29ybGQgamFja3NvbiE=" true null 2.2c world jac worl "hello world jackson!" "hello world jackson!" {"user":{"name":"jackson","sex":true,"age":22},"infos":[22,"this is array"]} {"user":{"address":"address","name":"haha","id":1,"birthday":null,"email":"email"},"infos":["a","b","c"]}
怎么样?构造的json字符串和输出的结果是一致的吧。关键看懂用JSONGenerator提供的方法,完成一个Object的构建。
三、JSON转换成Java对象
1、 将json字符串转换成JavaBean对象
@Testpublic void readJson2Entity() { String json = "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}"; try { AccountBean acc = objectMapper.readValue(json, AccountBean.class); System.out.println(acc.getName()); System.out.println(acc); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
很简单,用到了ObjectMapper这个对象的readValue这个方法,这个方法需要提供2个参数。第一个参数就是解析的JSON字符串,第二个参数是即将将这个JSON解析吃什么Java对象,Java对象的类型。当然,还有其他相同签名方法,如果你有兴趣可以一一尝试使用方法,当然使用的方法和当前使用的方法大同小异。运行后,结果如下:
hahahaha#1#address#null#email
2、 将json字符串转换成List<Map>集合
/** * <b>function:</b>json字符串转换成list<map> * @author hoojo * @createDate 2010-11-23 下午06:12:01 */@Testpublic void readJson2List() { String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+ "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; try { List<LinkedHashMap<String, Object>> list = objectMapper.readValue(json, List.class); System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { Map<String, Object> map = list.get(i); Set<String> set = map.keySet(); for (Iterator<String> it = set.iterator();it.hasNext();) { String key = it.next(); System.out.println(key + ":" + map.get(key)); } } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
尝试过将上面的JSON转换成List,然后List中存放AccountBean,但结果失败了。但是支持Map集合。因为你转成List.class,但是不知道List存放何种类型。只好默然Map类型。因为所有的对象都可以转换成Map结合,运行后结果如下:
2address:address2name:haha2id:2email:email2address:addressname:hahaid:1email:email
3、 Json字符串转换成Array数组,由于上面的泛型转换不能识别到集合中的对象类型。所有这里用对象数组,可以解决这个问题。只不过它不再是集合,而是一个数组。当然这个不重要,你可以用Arrays.asList将其转换成List即可。
/** * <b>function:</b>json字符串转换成Array * @author hoojo * @createDate 2010-11-23 下午06:14:01 */@Testpublic void readJson2Array() { String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+ "{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; try { AccountBean[] arr = objectMapper.readValue(json, AccountBean[].class); System.out.println(arr.length); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
运行后的结果:
2haha2#2#address2#null#email2haha#1#address#null#email
4、 Json字符串转换成Map集合
/** * <b>function:</b>json字符串转换Map集合 * @author hoojo * @createDate Nov 27, 2010 3:00:06 PM */@Testpublic void readJson2Map() { String json = "{\"success\":true,\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},"+ "\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}"; try { Map<String, Map<String, Object>> maps = objectMapper.readValue(json, Map.class); System.out.println(maps.size()); Set<String> key = maps.keySet(); Iterator<String> iter = key.iterator(); while (iter.hasNext()) { String field = iter.next(); System.out.println(field + ":" + maps.get(field)); } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
运行后结果如下:
3success:trueA:{address=address2, name=haha2, id=2, email=email2}B:{address=address, name=haha, id=1, email=email}
四、Jackson对XML的支持
Jackson也可以完成java对象到xml的转换,转换后的结果要比json-lib更直观,不过它依赖于stax2-api.jar这个jar包。