Jackson 学习笔记

语法

  1. com.fasterxml.jackson.databind.ObjectMapper ObjectMapper mapper; 不要每次都new如果在Spring中直接注入使用。
  2. JsonNode不能put可以强转objectNode后操作。
  3. objectNode添加内容时。基础类型、包装类用 put,自定义对象用set
方法 参数 返回 介绍
mapper.writeValueAsString 对象 json字符串
mapper.readValue jsonString, class 对象 用于知晓对象类型的场景
mapper.readValue jsonString, JavaType 字符串转集合(结果要强转)
mapper.readValue jsonString, TypeReference<T> 字符串转集合
mapper.readTree json字符串 JsonNode 用于类型不明的场景

测试用对象

public class Npc { private String name;private Integer age;}

JSONPOJO

String jsonStr = "{\"name\":\"张三\", \"age\":18}";
Npc npc = mapper.readValue(jsonStr, Npc.class);

System.out.print("json2obj:");System.out.println(npc);

POJOJSON

String obj2json = mapper.writeValueAsString(npc);

System.out.print("obj2json:");System.out.println(obj2json);

JSONlist

String json2list = "[{\"name\":\"李四\",\"age\":13},{\"name\":\"王五\",\"age\":14},{\"name\":\"赵六\",\"age\":15},{\"name\":\"洪七\",\"age\":16},{\"name\":\"重八\",\"age\":17}]";

// 方案一:有缺陷结果需要强转不好看
JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Npc.class);
List<Npc> npcs = (List<Npc>) mapper.readValue(json2list, javaType);

System.out.print("json2list1:");System.out.println(npcs);

// 方案二:无需要强转
TypeReference<List<Npc>> listTypeReference = new TypeReference<List<Npc>>(){};
List<Npc> npcs1 = mapper.readValue(json2list, listTypeReference);

System.out.print("json2list2:");System.out.println(npcs1);

listJSON

List<Npc> list = new ArrayList<Npc>();
list.add(new Npc("李四", 13));
list.add(new Npc("王五", 13));
list.add(new Npc("赵六", 13));
list.add(new Npc("洪七", 13));
list.add(new Npc("重八", 13));

String list2json = mapper.writeValueAsString(list);

System.out.print("list2json:");System.out.println(list2json);

手动构建json对象

还可以用 toPrettyString()输出格式化json

ArrayNode jsonNodes = mapper.createArrayNode(); // 创建json数组
for (int i = 0; i < 10; i++) {
   ObjectNode objectNode = mapper.createObjectNode(); // 创建json对象
   objectNode.put("name", "角色" + i);
   jsonNodes.add(objectNode);
}

System.out.print("手动构建json对象:");
System.out.println(jsonNodes.toString());

at 按路径取直接目标值


String jsonStr2 = "[{\"name\":\"张三\",\"age\":13},{\"name\":\"重八\",\"age\":18,\"friend\":{\"name\":\"洪七\",\"age\":17,\"friend\":{\"name\":\"赵六\",\"age\":16,\"friend\":{\"name\":\"王五\",\"age\":15}}}}]";
ArrayNode jsonNodes1 = mapper.readValue(jsonStr2, ArrayNode.class);

System.out.print("at按路径取直接目标值:");
System.out.println(jsonNodes1.get(1).at("/friend/friend/friend/name"));

取值转类型

jsonNodes1.get(1).at("/friend/name").getNodeType(); // STRING
jsonNodes1.get(1).at("/friend/agt").getNodeType(); // NUMBER
String name = jsonNodes1.get(0).get("name").asText(); // 取值并转为字符串类型
int age = jsonNodes1.get(0).get("age").asInt(); // 取值并转为数字类型

注解

注解 说明 用于
@JsonInclude(Include.NON_EMPTY) 字段为null不序列化。 字段
@JsonIgnore 序列化时忽略此字段。 字段
@JsonProperty(value = “name”) 默认使用属性名,此注解可以自己定义序列华的名字。 字段
@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”) 设置时间格式。 字段
@JsonUnwrapped 把对象展平(消除层级放到同一级) 字段
@JsonIgnoreType 类注解,序列化时忽略此类。
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = “id”) 用于指示在序列化/反序列化值时可以正确地反序列化对单个Java对象的多个引用。用于处理循环对象引用。
@JsonPropertyOrder({“field1”, “field2”, “field2”}) 设置字段顺序
@JsonIgnoreProperties({“field1”, “field2”}) 序列化时忽略“field1”, “field2” 两个属性。反序列化时忽略无Getter 和 Setter 的属性。

参考资料

github: jackson-databind
github: jackson-annotations

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章