jackson 是一個 java json 庫,提供了完備的 json 解析,序列化以及反序列化功能java
在 build.gradle 裏面添加依賴配置node
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'
String jsonString = "{\"name\": \"hatlonely\" /* comment */, \"birthday\": \"2018-03-18 15:26:37\", \"mails\": [\"hatlonely@foxmail.com\", \"hatlonely@gmail.com\"]}"; 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("hatlonely@foxmail.com")); assertThat(node.path("mails").path(1).asText(), equalTo("hatlonely@gmail.com"));
調用 ObjectMapper.readTree
就能講 json 字符串解析成一個 JsonNode
對象,而後經過 path
方法就能夠獲取 json 中各個字段的值了,這種方式能夠用來讀取 json 格式的配置文件,能夠用一個 JsonFactory 打開 ALLOW_COMMENTS 特性,能夠在 json 裏面加入註釋git
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 中的字段名github
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("hatlonely@foxmail.com", "hatlonely@gmail.com")); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(person); assertThat(jsonString, equalTo( "{\"name\":\"hatlonely\",\"birthday\":\"2018-03-18 03:26:37\",\"mails\":[\"hatlonely@foxmail.com\",\"hatlonely@gmail.com\"]}"));
使用 ObjectMapper.writeValueAsString
方法就能夠序列化成 stringgolang
String jsonString = "{\"name\": \"hatlonely\", \"birthday\": \"2018-03-18 15:26:37\", \"mails\": [\"hatlonely@foxmail.com\", \"hatlonely@gmail.com\"]}"; 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("hatlonely@foxmail.com", "hatlonely@gmail.com")));
使用 ObjectMapper.readValue
方法就能實現反序列化,能夠經過 configure
方法設置碰到未知的屬性不拋異常json
轉載請註明出處
本文連接: http://hatlonely.com/2018/03/...