首页 > 编程语言 >[Java] jackson 和 fastjson 处理 JSON对比

[Java] jackson 和 fastjson 处理 JSON对比

时间:2022-10-15 11:22:05浏览次数:56  
标签:fastjson Info jackson age json JSON 解析

JSON字符串 -> 对象

处理原型数值

实体类

@Setter
@Getter
@ToString
public class Info {
    private int age;
}

测试代码

@Test
public void testString() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String[] jsons = {"{}", "{\"age\": null}", "{\"age\": \"\"}"};
    for (String json : jsons) {
        System.out.println("待处理json:" + json);
        System.out.printf("jackson解析结果:%s\n", objectMapper.readValue(json, Info.class));
        System.out.printf("fastjson解析结果:%s\n----------------------" +
                "\n", JSON.parseObject(json, Info.class));
    }
}

运行结果

待处理json:{}
jackson解析结果:Info(age=0)
fastjson解析结果:Info(age=0)
----------------------
待处理json:{"age": null}
jackson解析结果:Info(age=0)
fastjson解析结果:Info(age=0)
----------------------
待处理json:{"age": ""}
jackson解析结果:Info(age=0)
fastjson解析结果:Info(age=0)
----------------------

Process finished with exit code 0

标签:fastjson,Info,jackson,age,json,JSON,解析
From: https://www.cnblogs.com/yorkey/p/16793781.html

相关文章