java json 庫之 jackson
阿新 • • 發佈:2018-03-18
文件 反序 lang www. lob one 一個 post 屬性
jackson 是一個 java json 庫,提供了完備的 json 解析,序列化以及反序列化功能
依賴配置
在 build.gradle 裏面添加依賴配置
compile group: ‘com.fasterxml.jackson.core‘, name: ‘jackson-core‘, version: ‘2.9.4‘ compile group: ‘com.fasterxml.jackson.core‘, name: ‘jackson-databind‘, version: ‘2.9.4‘ compile group: ‘com.fasterxml.jackson.core‘, name: ‘jackson-annotations‘, version: ‘2.9.4‘
json 解析
String jsonString = "{\"name\": \"hatlonely\" /* comment */, \"birthday\": \"2018-03-18 15:26:37\", \"mails\": [\"[email protected]\", \"[email protected]\"]}"; JsonFactory jsonFactory = new JsonFactory(); jsonFactory.enable(Feature.ALLOW_COMMENTS); ObjectMapper objectMapper = new ObjectMapper(jsonFactory); JsonNode node = objectMapper.readTree(jsonString); assertThat(node.path("name").asText(), equalTo("hatlonely")); assertThat(node.path("birthday").asText(), equalTo("2018-03-18 15:26:37")); assertThat(node.path("mails").size(), equalTo(2)); assertThat(node.path("mails").path(0).asText(), equalTo("[email protected]")); assertThat(node.path("mails").path(1).asText(), equalTo("[email protected]"));
調用 ObjectMapper.readTree
就能講 json 字符串解析成一個 JsonNode
對象,然後通過 path
方法就可以獲取 json 中各個字段的值了,這種方式可以用來讀取 json 格式的配置文件,可以用一個 JsonFactory 打開 ALLOW_COMMENTS 特性,可以在 json 裏面加入註釋
序列化與反序列化
首先定義一個對象
class Person { String name; @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss") Date birthday; @JsonProperty("mails") List<String> emails; // 省略了 getter/setter }
除了支持基本的數據類型,還支持 List 和 Map 類型,甚至還支持 Date 類型,Date 類型默認的格式是 ISO8601 格式,也可以通過 @JsonFormat
指定日期格式,通過 @JsonProperty
指定字段在 json 中的字段名
序列化
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Person person = new Person();
person.setName("hatlonely");
person.setBirthday(dateFormat.parse("2018-03-18 15:26:37"));
person.setEmails(Arrays.asList("[email protected]", "[email protected]"));
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(person);
assertThat(jsonString, equalTo(
"{\"name\":\"hatlonely\",\"birthday\":\"2018-03-18 03:26:37\",\"mails\":[\"[email protected]\",\"[email protected]\"]}"));
使用 ObjectMapper.writeValueAsString
方法就可以序列化成 string
反序列化
String jsonString = "{\"name\": \"hatlonely\", \"birthday\": \"2018-03-18 15:26:37\", \"mails\": [\"[email protected]\", \"[email protected]\"]}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Person person = objectMapper.readValue(jsonString, Person.class);
assertThat(person.getName(), equalTo("hatlonely"));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
assertThat(person.getBirthday(), equalTo(dateFormat.parse("2018-03-18 15:26:37")));
assertThat(person.getEmails(), equalTo(Arrays.asList("[email protected]", "[email protected]")));
使用 ObjectMapper.readValue
方法就能實現反序列化,可以通過 configure
方法設置碰到未知的屬性不拋異常
參考鏈接
- Jackson JSON Tutorial:http://www.baeldung.com/jackson
- Jackson maven 倉庫:http://mvnrepository.com/search?q=jackson
- 測試代碼鏈接:https://github.com/hatlonely/hellojava/blob/master/src/test/java/jackson/JacksonTest.java
轉載請註明出處
本文鏈接:http://hatlonely.com/2018/03/18/java-json-%E5%BA%93%E4%B9%8B-jackson/
java json 庫之 jackson