参考
https://zhuanlan.zhihu.com/p/325726265
https://baeldung-cn.com/jackson-annotations
https://www.baeldung.com/jackson-object-mapper-tutorial
基本使用
将对象序列化成 json
Person person = new Person("John", "Doe", 25, "123 Main St");
ObjectMapper mapper = new ObjectMapper();
final String jsonStr = mapper.writeValueAsString(person);
将 json 反序列化为对象
String json = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":18,\"address\":\"123 Main St\"}";
final ObjectMapper objectMapper = new ObjectMapper();
final Person person = objectMapper.readValue(json, Person.class);
将 json 列表序列化为 List:
String json = "[\n" +
" {\n" +
" \"firstName\": \"tom\",\n" +
" \"lastName\": \"cat\",\n" +
" \"age\": 18,\n" +
" \"address\": \"789 Elm St\"\n" +
" },\n" +
" {\n" +
" \"firstName\": \"marry\",\n" +
" \"lastName\": \"cat\",\n" +
" \"age\": 23,\n" +
" \"address\": \"7 Elm St\"\n" +
" }\n" +
"]\n";
ObjectMapper mapper = new ObjectMapper();
// @formatter:off
final List<Person> personList = mapper.readValue(json, new TypeReference<List<Person>>() {});
// @formatter:on
System.out.println(personList);
处理复杂的 json
String jsonString = "{\n" +
" \"name\": \"ComplexJSON\",\n" +
" \"version\": 1.0,\n" +
" \"isActive\": true,\n" +
" \"categories\": [\"fiction\", \"technology\", \"science\"],\n" +
" \"metadata\": {\n" +
" \"author\": \"John Doe\",\n" +
" \"publishedYear\": 2022,\n" +
" \"ratings\": [4.5, 5.0, 3.8],\n" +
" \"comments\": [\n" +
" {\n" +
" \"user\": \"Alice\",\n" +
" \"text\": \"Great book!\"\n" +
" },\n" +
" {\n" +
" \"user\": \"Bob\",\n" +
" \"text\": \"Interesting content.\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"tags\": [\"java\", \"json\", \"nested\", \"complex\"],\n" +
" \"price\": 29.99\n" +
"}";
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonString);
// 获取 "Bob" 的值
String bobValue = rootNode
.path("metadata")
.path("comments")
.get(1)
.path("user")
.asText();
System.out.println(bobValue);
} catch (Exception ignored) {
}
高级使用
设置序列化配置
// 如果 json 中有的字段其对应的 java 类的字段是没有的,反序列化为 java 对象时不要报错
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 只序列化值非 null 的字段
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 序列化所有字段(默认)
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
// 只序列化值不为空的字段
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// 只序列化值不为其值类型默认值的字段
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
设置日期格式化样式
final Product product = new Product("Hamburger", new Date(), 12.0, 232, new ArrayList<>());
ObjectMapper mapper = new ObjectMapper();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(df);
System.out.println(mapper.writeValueAsString(product));
注解
和序列化相关的注解:
注解 | 作用 |
---|---|
@JsonPropertyOrder | 设置字段序列化后的排列顺序 |
@JsonValue | 用于设置哪个字段作为该类的序列化结果 |
@JsonSerialize | 设置自定义序列化器 |
和反序列化相关的注解:
注解 | 作用 |
---|---|
@JsonSetter | 放在 set 方法上,指定反序列化时从 json 中提取字段值使用的字段名 |
@JsonDeserialize | 设置自定义反序列化器 |
@JsonAlias | 设置反序列化时字段的一个或多个别名 |
通用注解:
注解 | 作用 |
---|---|
@JsonIgnoreProperties | 用在类上,设置忽略的字段 |
@JsonIgnore | 用在字段上,设置忽略当前字段 |
@JsonInclude | 设置排除值为空、为 null 等其他值的字段 |
@JsonFormat | 指定字段值序列化时的格式模板 |
@JsonUnwrapped | 设置将当前字段扁平化 |
@JsonProperty | 设置序列化为 json 后的字段名,设置从 json 反序列化时使用的字段名 |
源代码
本文的源代码在这:Gitee仓库