JSON注解


jackson的maven依賴

pom文件中的引用:

 <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-core</artifactId>
     <version>2.4.0</version>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-annotations</artifactId>
     <version>2.4.0</version>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.4.0</version>
  </dependency>
  <dependency>
   <groupId>com.sunline.ark</groupId>
   <artifactId>ark-support</artifactId>
  </dependency>
  </dependencies>
</project>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.3</version>
</dependency>

所以引入這一個依賴就可以了

@JsonProperty 此注解用於屬性上,作用是把該屬性的名稱序列化為另外一個名稱,如把trueName屬性序列化為name,@JsonProperty(value="name")。

復制代碼
import com.fasterxml.jackson.annotation.JsonProperty;

public class Student {

    @JsonProperty(value = "real_name")
    private String realName;

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "realName='" + realName + '\'' +
                '}';
    }
}
復制代碼

測試

復制代碼
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setRealName("zhangsan");
        System.out.println(new ObjectMapper().writeValueAsString(student));
    }
}
復制代碼

結果

{"real_name":"zhangsan"}

這里需要注意的是將對象轉換成json字符串使用的方法是fasterxml.jackson提供的!!

如果使用fastjson呢?

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
</dependency>
復制代碼
import com.alibaba.fastjson.JSON;

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.setRealName("zhangsan");
        System.out.println(JSON.toJSONString(student));
    }
}
復制代碼

結果

{"realName":"zhangsan"}

可以看到,@JsonProperty(value = "real_name")沒有生效,為啥?

因為fastjson不認識@JsonProperty注解呀!所以要使用jackson自己的序列化工具方法!

 --------------------------

@JsonProperty不僅僅是在序列化的時候有用,反序列化的時候也有用,比如有些接口返回的是json字符串,命名又不是標准的駝峰形式,在映射成對象的時候,將類的屬性上加上@JsonProperty注解,里面寫上返回的json串對應的名字
復制代碼
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        String jsonStr = "{\"real_name\":\"zhangsan\"}";
        Student student = new ObjectMapper().readValue(jsonStr.getBytes(), Student.class);
        System.out.println(student);
    }
}
復制代碼

結果:

Student{realName='zhangsan'}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM