使用版本爲2.6.4java
網上已經有挺多教程了來着,而後是用的之前的版本,這裏作下新的版本的筆記。 關於jackson
的優勢主要參考 點我看優勢喲!node
jacksonjar
介紹git
jackson-core
(必須包含),提供流模式jackson-databind
數據綁定包,提供對象綁定與數模型jackson-annotations
註解包,提供註解使用maven下載jackson相關jar:github
<!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.4</version> </dependency>
如下用demo的形式。介紹經常使用的功能;json
demo
之前初始化後續要用的各類變量,以及設置,Student
類就不貼了;app
ObjectMapper mapper; // can reuse, share globally JsonNodeFactory nodeFactory; JsonFactory jsonFactory; JsonGenerator generator; @Before public void mapper(){ mapper = new ObjectMapper(); /*設置輸出顯示爲json格式*/ mapper.configure(SerializationFeature.INDENT_OUTPUT, true); /*設置JSON按照name排序*/ mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); /*設置date樣式*/ SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy"); mapper.setDateFormat(outputFormat); /*設置忽略空的值*/ mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); /*用tree model 模式構造json*/ /*設置節點工廠,用於參數節點*/ nodeFactory = new JsonNodeFactory(false); /* 創造工廠將JSON節點生成爲JSON字符串 設置輸出爲System.out*/ jsonFactory = new JsonFactory(); try { generator = jsonFactory.createGenerator(out); } catch (IOException e) { e.printStackTrace(); } }
demo1
jsp
/** * Bean to JSON * result:{"name":"jackson","age":25,"course":{"number":12}} */ @Test public void demo1(){ try { Student student = new Student(); Course course = new Course(); course.setNumber(12); student.setAge(25); student.setName("jackson"); student.setCourse(course); System.out.print(mapper.writeValueAsString(student)); } catch (IOException e) { e.printStackTrace(); } }
demo2
maven
/** * Map To JSON * result:{"name":"jackson","age":25} */ @Test public void demo2(){ try { Map<String,Object> map = new HashMap<String, Object>(); map.put("name","jackson"); map.put("age",25); System.out.print(mapper.writeValueAsString(map)); } catch (IOException e) { e.printStackTrace(); } }
demo3
code
/** * List To JSON * Result:["jackson",25] */ @Test public void demo3(){ try{ List<Object> list = new ArrayList<Object>(); list.add("jackson"); list.add(25); System.out.println(mapper.writeValueAsString(list)); }catch (IOException e){ e.printStackTrace(); } }
demo4
orm
/** * List Map To JSON * Result:{"name":"jackson","course":[{"math":98},{"english":100}],"age":25} */ @Test public void demo4(){ try { Map<String,Object> map = new HashMap<String, Object>(); map.put("name","jackson"); map.put("age",25); List<Object> list = new ArrayList<Object>(); Map<String,Object> mapCourse1 = new HashMap<String, Object>(); mapCourse1.put("math",98); Map<String,Object> mapCourse2 = new HashMap<String, Object>(); mapCourse2.put("english",100); list.add(mapCourse1); list.add(mapCourse2); map.put("course",list); System.out.println(mapper.writeValueAsString(map)); }catch (IOException e){ e.printStackTrace(); } }
demo5
/** * Date formate set * result:{"birthday" : "23-03-2012"} */ @Test public void demo5(){ Map<String,Date> map = new HashMap<String, Date>(); SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); try { map.put("birthday",format.parse("23-03-2012")); } catch (ParseException e) { e.printStackTrace(); } try { out.println(mapper.writeValueAsString(map)); } catch (JsonProcessingException e) { e.printStackTrace(); } }
demo6
/** * Tree model 模式 * * Tree model to Json * * result:{"name":"jackson","major":{"mathnum":12},"addr":[{"addr":"cq"}]} */ @Test public void demo6(){ ObjectNode teacher = nodeFactory.objectNode(); teacher.put("name","jackson"); ObjectNode course = teacher.putObject("major"); course.put("mathnum",12); ArrayNode addr = teacher.putArray("addr"); ObjectNode detailAddr = nodeFactory.objectNode(); detailAddr.put("addr","cq"); addr.add(detailAddr); try { mapper.writeTree(generator, teacher); } catch (IOException e) { e.printStackTrace(); } }
demo7
/** * Tree model 模式 * * Json to Tree model and tree model to json */ @Test public void demo7(){ String jsonString = "{\"name\":\"jackson\",\"major\":{\"mathnum\":12},\"addr\":[{\"addr\":\"cq\"}]}"; ObjectNode teacher = null; try { teacher = (ObjectNode) mapper.readTree(jsonString); } catch (IOException e) { e.printStackTrace(); } out.println("name:"+teacher.get("name").asText()); ObjectNode major = (ObjectNode) teacher.get("major"); out.println("mathnum:"+major.get("mathnum").asInt()); ArrayNode addr = (ArrayNode) teacher.get("addr"); ObjectNode addr1 = (ObjectNode) addr.get(0); out.println("addr:"+addr1.get("addr").asText()); }
以上爲基本與用法,更高端的能夠參考: