Json解析工具Jackson(使用注解)--jackson框架自定义的一些json解析注解

  • @JsonIgnoreProperties

此注解是类注解,作用是json序列化时将Javabean中的一些属性忽略掉,序列化和反序列化都受影响。

  • @JsonIgnore

此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。

  • @JsonFormat

此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

  • @JsonSerialize

此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。

  1. public class CustomDoubleSerialize extends JsonSerializer<Double> {
  2. private DecimalFormat df = new DecimalFormat("##.00");
  3. @Override
  4. public void serialize(Double value, JsonGenerator jgen,
  5. SerializerProvider provider) throws IOException,
  6. JsonProcessingException {
  7. jgen.writeString(df.format(value));
  8. }
  9. }
  • @JsonDeserialize

此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize

  1. public class CustomDateDeserialize extends JsonDeserializer<Date> {
  2. private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. @Override
  4. public Date deserialize(JsonParser jp, DeserializationContext ctxt)
  5. throws IOException, JsonProcessingException {
  6. Date date = null;
  7. try {
  8. date = sdf.parse(jp.getText());
  9. } catch (ParseException e) {
  10. e.printStackTrace();
  11. }
  12. return date;
  13. }
  14. }
  • 完整例子
  1. //表示序列化时忽略的属性
  2. @JsonIgnoreProperties(value = { "word" })
  3. public class Person {
  4. private String name;
  5. private int age;
  6. private boolean sex;
  7. private Date birthday;
  8. private String word;
  9. private double salary;
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public int getAge() {
  17. return age;
  18. }
  19. public void setAge(int age) {
  20. this.age = age;
  21. }
  22. public boolean isSex() {
  23. return sex;
  24. }
  25. public void setSex(boolean sex) {
  26. this.sex = sex;
  27. }
  28. public Date getBirthday() {
  29. return birthday;
  30. }
  31. // 反序列化一个固定格式的Date
  32. @JsonDeserialize(using = CustomDateDeserialize.class)
  33. public void setBirthday(Date birthday) {
  34. this.birthday = birthday;
  35. }
  36. public String getWord() {
  37. return word;
  38. }
  39. public void setWord(String word) {
  40. this.word = word;
  41. }
  42. // 序列化指定格式的double格式
  43. @JsonSerialize(using = CustomDoubleSerialize.class)
  44. public double getSalary() {
  45. return salary;
  46. }
  47. public void setSalary(double salary) {
  48. this.salary = salary;
  49. }
  50. public Person(String name, int age) {
  51. this.name = name;
  52. this.age = age;
  53. }
  54. public Person(String name, int age, boolean sex, Date birthday,
  55. String word, double salary) {
  56. super();
  57. this.name = name;
  58. this.age = age;
  59. this.sex = sex;
  60. this.birthday = birthday;
  61. this.word = word;
  62. this.salary = salary;
  63. }
  64. public Person() {
  65. }
  66. @Override
  67. public String toString() {
  68. return "Person [name=" + name + ", age=" + age + ", sex=" + sex
  69. + ", birthday=" + birthday + ", word=" + word + ", salary="
  70. + salary + "]";
  71. }
  72. }
    1. public class Demo {
    2. public static void main(String[] args) {
    3. writeJsonObject();
    4. // readJsonObject();
    5. }
    6. // 直接写入一个对象(所谓序列化)
    7. public static void writeJsonObject() {
    8. ObjectMapper mapper = new ObjectMapper();
    9. Person person = new Person("nomouse", 25, true, new Date(), "程序员",
    10. 2500.0);
    11. try {
    12. mapper.writeValue(new File("c:/person.json"), person);
    13. } catch (JsonGenerationException e) {
    14. e.printStackTrace();
    15. } catch (JsonMappingException e) {
    16. e.printStackTrace();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. // 直接将一个json转化为对象(所谓反序列化)
    22. public static void readJsonObject() {
    23. ObjectMapper mapper = new ObjectMapper();
    24. try {
    25. Person person = mapper.readValue(new File("c:/person.json"),
    26. Person.class);
    27. System.out.println(person.toString());
    28. } catch (JsonParseException e) {
    29. e.printStackTrace();
    30. } catch (JsonMappingException e) {
    31. e.printStackTrace();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. }  
      1. 本文转自http://blog.csdn.net/nomousewch/article/details/8955796 感谢作者

最新文章

  1. STL学习之运算符(&lt;&lt;)重载问题和仿函数的实现
  2. SQL基础--索引
  3. jquery实现搜索提示效果
  4. 【转】C#多线程示例
  5. sql_树形查询
  6. R6010 - abort() has been called 错误
  7. Backbone学习笔记一Backbone中的MVC
  8. javascript中的 类初始化,遍历for in 以及with的用法
  9. js模块化编程总结
  10. VMware linux与windows文件共享
  11. HDU 4763 (KMP算法)
  12. 【转】FLV视频封装格式详解
  13. vim自动补全文章搜集
  14. Vim的基本使用(一)
  15. New FileReader上传图片
  16. 使用Setup factory打包WPF
  17. .NET Core 源码导航(按程序集链接)
  18. Apache coredump 问题发现与解决记录
  19. 【mysql】GitHub 的 MySQL 高可用性实践分享
  20. Yii2增删改查

热门文章

  1. 国内使用pip / pip with GFW / pip 镜像
  2. jQuery学习笔记(2)-选择器的使用
  3. cocos2d-x win7 部署
  4. 扩展Snackbar 使其支持居中显示
  5. SugarCRM安装过程——PHP文件上传限制问题
  6. [oracle]表空间情况查看、占用、扩容、使用情况、空间维护等操作
  7. linux mysql设置远程访问
  8. [转载] Linux Futex的设计与实现
  9. 3D赛瓦号——整装待发!
  10. h5页面长按保存图片