admin 管理员组文章数量: 1086019
2024年12月27日发(作者:app用户登录界面)
Json解析工具Jackson(使用注解)
•
@JsonIgnoreProperties
此注解是类注解,作用是json序列化时将java bean中的一些属
性忽略掉,序列化和反序列化都受影响。
•
@JsonIgnore
此注解用于属性或者方法上(最好是属性上),作用和上面的
@JsonIgnoreProperties一样。
•
@JsonFormat
此注解用于属性或者方法上(最好是属性上),可以方便的把
Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern =
"yyyy-MM-dd HH-mm-ss")
•
@JsonSerialize
此注解用于属性或者getter方法上,用于在序列化时嵌入我们自
定义的代码,比如序列化一个double时在其后面限制两位小数点。
[java] view plaincopy
1. public class CustomDoubleSerialize extends JsonSerializ
er
2.
3. private DecimalFormat df = new DecimalFormat("##.00")
;
4.
5. @Override
6. public void serialize(Double value, JsonGenerator jgen,
7. SerializerProvider provider) throws IOException,
8. JsonProcessingException {
9.
10. tring((value));
11. }
12. }
•
@JsonDeserialize
此注解用于属性或者setter方法上,用于在反序列化时可以嵌入
我们自定义的代码,类似于上面的@JsonSerialize
[java] view plaincopy
1. public class CustomDateDeserialize extends JsonDeserial
izer
2.
3. private SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd");
4.
5. @Override
6. public Date deserialize(JsonParser jp, DeserializationCont
ext ctxt)
7. throws IOException, JsonProcessingException {
8.
9. Date date = null;
10. try {
11. date = (t());
12. } catch (ParseException e) {
13. tackTrace();
14. }
15. return date;
16. }
17. }
•
完整例子
[java] view plaincopy
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.
11. public String getName() {
12. return name;
13. }
14.
15. public void setName(String name) {
16. = name;
17. }
18.
19. public int getAge() {
20. return age;
21. }
22.
23. public void setAge(int age) {
24. = age;
25. }
26.
27. public boolean isSex() {
28. return sex;
29. }
30.
31. public void setSex(boolean sex) {
32. = sex;
33. }
34.
35. public Date getBirthday() {
36. return birthday;
37. }
38.
39. // 反序列化一个固定格式的Date
40. @JsonDeserialize(using =
s)
41. public void setBirthday(Date birthday) {
42. ay = birthday;
43. }
44.
45. public String getWord() {
46. return word;
47. }
48.
49. public void setWord(String word) {
50. = word;
51. }
52.
53. // 序列化指定格式的double格式
54. @JsonSerialize(using = )
55. public double getSalary() {
56. return salary;
57. }
58.
59. public void setSalary(double salary) {
60. = salary;
61. }
62.
63. public Person(String name, int age) {
64. = name;
65. = age;
66. }
67.
68. public Person(String name, int age, boolean sex, Date
birthday,
69. String word, double salary) {
70. super();
71. = name;
72. = age;
73. = sex;
74. ay = birthday;
75. = word;
76. = salary;
77. }
78.
79. public Person() {
80. }
81.
82. @Override
83. public String toString() {
84. return "Person [name=" + name + ", age=" + age + ",
sex=" + sex
85. + ", birthday=" + birthday + ", word=" + word + ", sal
ary="
86. + salary + "]";
87. }
88.
89. }
[java] view plaincopy
1. public class Demo {
2. public static void main(String[] args) {
3.
4. writeJsonObject();
5.
6. // readJsonObject();
7. }
8.
9. // 直接写入一个对象(所谓序列化)
10. public static void writeJsonObject() {
11. ObjectMapper mapper = new ObjectMapper();
12. Person person = new Person("nomouse", 25, true, ne
w Date(), "程序员",
13. 2500.0);
14. try {
15. alue(new File("c:/"), person);
16. } catch (JsonGenerationException e) {
17. tackTrace();
18. } catch (JsonMappingException e) {
19. tackTrace();
20. } catch (IOException e) {
21. tackTrace();
22. }
23. }
24.
25. // 直接将一个json转化为对象(所谓反序列化)
26. public static void readJsonObject() {
27. ObjectMapper mapper = new ObjectMapper();
28.
29. try {
30. Person person = lue(new File("c:/perso
"),
31. );
32. n(ng());
33. } catch (JsonParseException e) {
34. tackTrace();
35. } catch (JsonMappingException e) {
36. tackTrace();
37. } catch (IOException e) {
38. tackTrace();
39. }
40. }
41.
42.
43. }
版权声明:本文标题:Json解析工具Jackson(使用注解) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1735336544a1649942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论