设为首页 - 加入收藏 焦点技术网
热搜:java
当前位置:首页 >

Jackson转换Json

2016-04-03 16:06:42.0 java  
导读:Jackson转换Json。。。
##Jackson转换Json## ###依赖的jar包### **jackson包** jackson-annotations-2.6.0.jar jackson-core-2.6.3.jar jackson-databind-2.6.3.jar ###ObjectMapping和JsonGenerator### **ObjectMapping** ObjectMapper objectMapper = new ObjectMapper(); **JsonGenerator** 输出前需要flush(),使用后需要close JsonGenerator jsonGenerator = jsonGenerator = objectMapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8); if (jsonGenerator != null) { jsonGenerator.flush(); } if (!jsonGenerator.isClosed()) { jsonGenerator.close(); } **POJO** AccountBean bean = new AccountBean(); bean.setAddress("china-Guangzhou"); bean.setEmail("hoojo_@126.com"); bean.setId(1); bean.setName("hoojo"); ------------------------------------------------------------ ###POJO转换成json### **ObjectMapping** objectMapper.writeValue(System.out, bean); **JsonGenerator** jsonGenerator.writeObject(bean); 输出:{"id":1,"name":"hoojo","email":"hoojo_@126.com","address":"china-Guangzhou","birthday":null} ###json转换成POJO### String json="{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}"; AccountBean accountBean = objectMapper.readValue(json, AccountBean.class); ------------------------------------------------------------ ###List转换成json### **ObjectMapping** String value = objectMapper.writeValueAsString(list) //或 objectMapper.writeValue(System.out, list); **JsonGenerator** jsonGenerator.writeObject(list); [{"id":1,"name":"hoojo","email":"hoojo_@126.com","address":"china-Guangzhou","birthday":null},{"id":2,"name":"haha2","email":"email2","address":"address2","birthday":null}] ###json转换成数组### String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; AccountBean[] accountBeen = new ObjectMapper().readValue(json,AccountBean[].class); ###json转换成List### String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, AccountBean.class); List<AccountBean> list = objectMapper.readValue(json, javaType); ###json转换成List&lt;Map&gt;### String json = "[{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"},{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}]"; List<HashMap<String,Object>> list = objectMapper.readValue(json, List.class); for(int i=0; i< list.size(); i++){ HashMap<String, Object> hashMap = list.get(i); Iterator<Map.Entry<String, Object>> entryIterator = hashMap.entrySet().iterator(); while(entryIterator.hasNext()){ Map.Entry<String, Object> entry = entryIterator.next(); System.out.println(entry.getKey() +"="+ entry.getValue()); } } ------------------------------------------------------------ ###Map转换成json### **ObjectMapping** objectMapper.writeValue(System.out, map); **JsonGenerator** jsonGenerator.writeObject(map); {"flag":"true","account1":{"id":0,"name":null,"email":"hoojo@qq.com","address":"china-Beijin","birthday":null},"account2":{"id":1,"name":"hoojo","email":"hoojo_@126.com","address":"guangzhou","birthday":null}} ###json转换成Map### String json = "{\"A\":{\"address\": \"address2\",\"name\":\"haha2\",\"id\":2,\"email\":\"email2\"}," +"\"B\":{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}}"; **第一种**:解析成String-Object //第二个参数是Object Map<String, Object> maps = new ObjectMapper().readValue(json, Map.class); while(entryIterator.hasNext()){ Map.Entry<String, Object> entry = entryIterator.next(); System.out.println(entry.getKey() + "=" + entry.getValue()); } **第二种**:解析成String-POJO JavaType javaType = objectMapper.getTypeFactory().constructMapType(HashMap.class, String.class, AccountBean.class); Map<String,AccountBean> map = objectMapper.readValue(json, javaType); Iterator<Map.Entry<String, AccountBean>> entryIterator = map.entrySet().iterator(); while(entryIterator.hasNext()){ Map.Entry<String,AccountBean> entry = entryIterator.next(); System.out.println(entry.getKey() +"__"); System.out.println(entry.getValue().getAddress()); } **第三种**:解析成String-Map Map<String, Map<String,Object>> maps = new ObjectMapper().readValue(json, Map.class); Iterator<Map.Entry<String,Map<String,Object>>> entryIterator = maps.entrySet().iterator(); while(entryIterator.hasNext()){ Map.Entry<String, Map<String,Object>> entry = entryIterator.next(); System.out.println(entry.getKey()+"*"); Map<String, Object> map = entry.getValue(); Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String, Object> entry1 = iterator.next(); System.out.println(entry1.getKey() + "#" + entry1.getValue()); } } ------------------------------------------------------------ ###复杂的json输出### String[] arr = {"a", "b", "c"}; String str = "hell oa <>&*&lt world jackson!\"address\": \"address2\",\"name\":\"haha2\""; //byte,null,boolean,number jsonGenerator.writeBinary(str.getBytes()); jsonGenerator.writeBoolean(true); jsonGenerator.writeNull(); jsonGenerator.writeNumber(2.2f); //String jsonGenerator.writeRawValue(str, 5, 5); jsonGenerator.writeRawValue(str); jsonGenerator.writeString(str);//前后有空格 //前后没有空格 jsonGenerator.writeRaw('c'); jsonGenerator.writeRaw(str); jsonGenerator.flush(); System.out.println(); //对象{"id":1,"name":"hoojo","email":"hoojo_@126.com","address":"china-Guangzhou","birthday":null} jsonGenerator.writeObject(bean); //对象{"user":{"id":1,"name":"hoojo","email":"hoojo_@126.com","address":"china-Guangzhou","birthday":null},"info":["a","b","c"]} jsonGenerator.writeStartObject(); jsonGenerator.writeObjectField("user", bean); jsonGenerator.writeObjectField("info", arr); jsonGenerator.writeEndObject(); jsonGenerator.flush(); System.out.println(); //复杂对象 {"user":{"id":1,"memo":"123344556677","name":"jackson"},"info":["a",22,true]} jsonGenerator.writeStartObject(); //{ jsonGenerator.writeObjectFieldStart("user");//user:{ jsonGenerator.writeFieldName("id");//id:1 jsonGenerator.writeNumber(1); jsonGenerator.writeFieldName("memo"); jsonGenerator.writeString("123344556677"); jsonGenerator.writeStringField("name","jackson");//name:jackson jsonGenerator.writeEndObject();//} //jsonGenerator.writeStartArray(); //[ jsonGenerator.writeArrayFieldStart("info");//info:[ jsonGenerator.writeString("a"); jsonGenerator.writeNumber(22);//22 jsonGenerator.writeBoolean(true); jsonGenerator.writeEndArray();//] jsonGenerator.writeEndObject();//} ###其他### **如果要输出的JSON好看点** System.out.println(objectMapper.defaultPrettyPrintingWriter().writeValueAsString(user)); 参考:http://blog.csdn.net/laixiaonian/article/details/8144920

(编辑: CrazyManDF)

网友评论
相关文章