Json之Jackson的使用

Jackson 是一个 Java 的用来处理 JSON 格式数据的类库,性能非常好,也是SpringMVC默认使用的库,所以学习它还是很有用的。

Jackson简单使用

  • 实体类
@Data
public class CoreAdmin implements Serializable {

    private static final long serialVersionUID = -2713607476733679931L;

    private Long id;
    @JsonProperty("user_name")
    private String username;
    @JsonIgnore
    private String password;
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm")
    private Date createTime;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private CoreAdmin coreAdmin;

}
  • 序列化
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    CoreAdmin coreAdmin = new CoreAdmin();
    coreAdmin.setId(0L);
    coreAdmin.setUsername("admin");
    coreAdmin.setPassword("admin");
    coreAdmin.setCreateTime(new Date());
    String jsonString = objectMapper.writeValueAsString(coreAdmin);
    System.out.println(jsonString);
    // {"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"}
}
  • 反序列,转成指定对象
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = "{\"id\":0,\"createTime\":\"2020-05-28 18:37\",\"user_name\":\"admin\"}";
    CoreAdmin coreAdmin = objectMapper.readValue(jsonString, CoreAdmin.class);
    System.out.println(coreAdmin);
    // CoreAdmin(id=0, username=admin, password=null, createTime=Thu May 28 18:37:00 CST 2020, coreAdmin=null)
}
  • 反序列,转成Json节点树
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = "{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\",\"coreAdmin\":{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\"}, \"coreAdminList\":[{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\"}]}";
    JsonNode jsonNode = objectMapper.readTree(jsonString);
    System.out.println(jsonNode);//{"id":0,"createTime":"2020-05-28 18:50","user_name":"admin","coreAdmin":{"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"},"coreAdminList":[{"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"}]}
    System.out.println(jsonNode.getClass().getName()); //com.fasterxml.jackson.databind.node.ObjectNode
    System.out.println(jsonNode.get("id"));// 0 对象通过字段名获取值
    System.out.println(jsonNode.get("id").getClass().getName());//com.fasterxml.jackson.databind.node.IntNode
    System.out.println(jsonNode.get("coreAdmin")); // {"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"}
    System.out.println(jsonNode.get("coreAdmin").getClass().getName());// com.fasterxml.jackson.databind.node.ObjectNode
    System.out.println(jsonNode.get("coreAdmin").get("id"));
    System.out.println(jsonNode.get("coreAdminList")); // [{"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"}]
    System.out.println(jsonNode.get("coreAdminList").getClass().getName());// com.fasterxml.jackson.databind.node.ArrayNode
    System.out.println(jsonNode.get("coreAdminList").get(0));// 数组通过索引获取值 {"id":0,"createTime":"2020-05-28 18:50","user_name":"admin"}
}	
	// 获取字段值
	JsonNode username1 = jsonNode.path("user_name");
    JsonNode username2 = jsonNode.findValue("user_name");
    JsonNode username3 = jsonNode.findPath("user_name");
    // JsonNode Text 转 String
    System.out.println(username1.asText());
    System.out.println(username2);
    System.out.println(username2);
    // 转成指定的集合类型
	List<User> users = objectMapper.readValue(json, new TypeReference<List<User>>() {});
  • 反序列化,转成Map
public static void main(String[] args) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = "{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\",\"coreAdmin\":{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\"}, \"coreAdminList\":[{\"id\":0,\"createTime\":\"2020-05-28 18:50\",\"user_name\":\"admin\"}]}";
    Map map = objectMapper.readValue(jsonString, Map.class);
    System.out.println(map);// {id=0, createTime=2020-05-28 18:50, user_name=admin, coreAdmin={id=0, createTime=2020-05-28 18:50, user_name=admin}, coreAdminList=[{id=0, createTime=2020-05-28 18:50, user_name=admin}]}
    System.out.println(map.getClass().getName());
    System.out.println(map.get("id"));// 0
    System.out.println(map.get("id").getClass().getName());// java.lang.Integer
    System.out.println(map.get("coreAdmin"));// {id=0, createTime=2020-05-28 18:50, user_name=admin}
    System.out.println(map.get("coreAdmin").getClass().getName());// java.util.LinkedHashMap
    System.out.println(map.get("coreAdminList"));// [{id=0, createTime=2020-05-28 18:50, user_name=admin}]
    System.out.println(map.get("coreAdminList").getClass().getName());// java.util.ArrayList
}

Jackson注解

  • @JsonProperty:json字符串的属性名,主要用于java实体属性的json属性的映射
  • @JsonIgnore:转化是忽略这个属性
  • @JsonFormat: 主要用于时间的格式化
  • @JsonInclude :序列化是,属性为空json字符串忽略显示这个字段
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值