The Java JSON Library of Jackson

Source: Internet
Author: User
Tags dateformat serialization throw exception

Jackson is a Java JSON library that provides complete JSON parsing, serialization, and deserialization capabilities

Dependency configuration

Add dependency configuration in 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 parsing
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]"));

The call ObjectMapper.readTree is able to parse the JSON string into an JsonNode object, and then the path method can get the value of each field in the JSON, which can be used to read the JSON format of the configuration file can be opened with a jsonfactory Allow_co Mments feature, you can add comments to the JSON

Serialization and deserialization first define an object
class Person {    String name;    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")    Date birthday;    @JsonProperty("mails")    List<String> emails;    // 省略了 getter/setter}

In addition to supporting basic data types, List and MAP types are supported, and even date types are supported, the default format for date type is ISO8601 format, or you can specify the date format by specifying the field @JsonFormat @JsonProperty name in the JSON

Serialization of
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]\"]}"));

Use the ObjectMapper.writeValueAsString method to serialize to string

Deserialization
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]")));

The method can be used to ObjectMapper.readValue achieve deserialization, can be configure set by the method encountered unknown property does not throw exception

Reference links
    • Jackson JSON Tutorial:http://www.baeldung.com/jackson
    • Jackson maven Warehouse: Http://mvnrepository.com/search?q=jackson
    • Test Code Link: Https://github.com/hatlonely/hellojava/blob/master/src/test/java/jackson/JacksonTest.java

Reprint please indicate the source
This article link: http://hatlonely.com/2018/03/18/java-json-%E5%BA%93%E4%B9%8B-jackson/

The Java JSON Library of Jackson

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.