background

Fastjson has been so buggy lately that people are turning to Jackson because they’re constantly being asked to upgrade at work

Basic usage

Maven rely on

        <! --jackson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.0</version>
        </dependency>
Copy the code

The basic use

    @Test
    public void testJackson(a) throws IOException {
        Map<String,String> map = new HashMap<String,String>();
        ObjectMapper objectMapper = new ObjectMapper();
        map.put("a"."aaaa");
        map.put("b"."bbbb");
        System.out.println("*** * Step1: Convert map to JSON ***");
        String jsonStr = objectMapper.writeValueAsString(map);
        System.out.println(jsonStr);

        System.out.println(*** step2: Convert string json to ***);
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(jsonStr);
        System.out.println("Get value:" + rootNode.get("a"));

    }
Copy the code