使用版本为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()); }
以上为基本与用法,更高端的能够参考: