zoukankan      html  css  js  c++  java
  • json中注解@JsonProperty用法

    jackson的maven依赖

    
     
    1. <dependency>

    2. <groupId>com.fasterxml.jackson.core</groupId>

    3. <artifactId>jackson-databind</artifactId>

    4. <version>2.5.3</version>

    5. </dependency>

    @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。

    
     
    1. import com.fasterxml.jackson.annotation.JsonProperty;

    2.  
    3. public class Student {

    4.  
    5. @JsonProperty("name")

    6. private String trueName;

    7.  
    8. public String getTrueName() {

    9. return trueName;

    10. }

    11.  
    12. public void setTrueName(String trueName) {

    13. this.trueName = trueName;

    14. }

    15. }

    测试一下

    
     
    1. import com.fasterxml.jackson.core.JsonProcessingException;

    2. import com.fasterxml.jackson.databind.ObjectMapper;

    3.  
    4. public class Main {

    5. public static void main(String[] args) throws JsonProcessingException {

    6. Student student = new Student();

    7. student.setTrueName("张三");

    8. System.out.println(new ObjectMapper().writeValueAsString(student));

    9. }

    10. }

    得到结果

    {"name":"张三"} 

    这里需要注意的是将对象转换成json字符串使用的方法是fasterxml.jackson提供的!!

    @JsonProperty不仅仅是在序列化的时候有用,反序列化的时候也有用,比如有些接口返回的是json字符串,命名又不是标准的驼峰形式,在映射成对象的时候,将类的属性上加上@JsonProperty注解,
    里面写上返回的json串对应的名字

    --------------------- 本文来自 美好的未来在于把握今天 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/liliang_11676/article/details/80210065?utm_source=copy

  • 相关阅读:
    【KVM系列 virt-v2v】virt-v2v过程中的报错
    大机与超级计算机的区别
    多路径 on linux
    ansible 管控 windows
    Linux启动盘
    RHCA 环境命令
    Xshell 4 连接 Ubuntu/Kali 报错 "找不到匹配的key exchange算法"
    security group & ACL
    windows powercfg
    OKD
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317466.html
Copyright © 2011-2022 走看看