FastJson中@JSONField注解使用

由于Json中的key与bean中的属性不能匹配,在转换过程中出现部分属性为null的情况,此时可以使用@JSONField进行解释。

参考:FastJson及Gson定义字段别名

@JSONField的作用对象:

  1. Field
  2. Setter 和 Getter方法

注:FastJson操作时,是根据Getter和Setter的方法进行的,并不是依据Field进行

Address.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
public class Address {
@JSONField(name = "address_id")
private long id;
@JSONField(name = "user_id")
private long userId;
private String name;
private String mobile;
private Region country;
private Region state;
private Region city;
private Region district;
@JSONField(name = "address_detail")
private String addressDetail;
@JSONField(name = "post_code")
private String postcode;
@JSONField(serialize = false)
private boolean isDefault;

public Address() {
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

@JSONField(serialize = false)
public Region getCountryRegion() {
return country;
}

@JSONField(name = "country")
public String getCountry() {
return country.getName();
}

@JSONField(serialize = false)
public void setCountryRegion(Region country) {
this.country = country;
}

@JSONField(name = "country")
public void setCountry(String country) {
Region region = new Region();
region.setName(country);
this.country = region;
}

@JSONField(serialize = false)
public Region getStateRegion() {
return state;
}

@JSONField(name = "state")
public String getState() {
return state.getName();
}

@JSONField(serialize = false)
public void setStateRegion(Region state) {
this.state = state;
}

@JSONField(name = "state")
public void setState(String state) {
Region region = new Region();
region.setName(state);
this.state = region;
}

@JSONField(serialize = false)
public Region getCityRegion() {
return city;
}

@JSONField(name = "city")
public String getCity() {
return city.getName();
}

@JSONField(serialize = false)
public void setCityRegion(Region city) {
this.city = city;
}

@JSONField(name = "city")
public void setCity(String city) {
Region region = new Region();
region.setName(city);
this.city = region;
}

@JSONField(serialize = false)
public Region getDistrictRegion() {
return district;
}

@JSONField(name = "district")
public String getDistrict() {
return district.getName();
}

@JSONField(serialize = false)
public void setDistrictRegion(Region district) {
this.district = district;
}

@JSONField(name = "district")
public void setDistrict(String district) {
Region region = new Region();
region.setName(district);
this.district = region;
}

public String getAddressDetail() {
return addressDetail;
}

public void setAddressDetail(String addressDetail) {
this.addressDetail = addressDetail;
}

public String getPostcode() {
return postcode;
}

public void setPostcode(String postcode) {
this.postcode = postcode;
}

public boolean isDefault() {
return isDefault;
}

public void setDefault(boolean aDefault) {
isDefault = aDefault;
}
}

Region.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Region {
private int id;
private String code;
private String name;
private int parentId;

public Region() {

}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getParentId() {
return parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :