專案中遇到後臺傳給前臺的json字串命名方式不符合java的命名規範如:{"exp_value":"exp1","user_age":11,"user_name":"username1"}javabean中是小駝峰命名:
private String userName;
private String expValue;
private int userAge
關於FastJson屬性別名的問題,網上很多文章都指出@JSONField(name = "***")必須要註解在setter和getter上面;但是實際測試compile "com.alibaba:fastjson:1.1.56"版本的FastJson發現@JSONField註解其實可以直接寫到屬性名上面,例項如下:
JavaBean
public class JsonTestBean {
@JSONField(name = "user_name")
@JSONField(name = "exp_value")
@JSONField(name = "user_age")
private int userAge;
public String getExpValue() {
return expValue;
}
public void setExpValue(String expValue) {
this.expValue = expValue;
public int getUserAge() {
return userAge;
public void setUserAge(int userAge) {
this.userAge = userAge;
public String getUserName() {
return userName;
public void setUserName(String userName) {
this.userName = userName;
@Override
public String toString() {
return "JsonTestBean{" +
"expValue="" + expValue + "\"" +
", userName="" + userName + "\"" +
", userAge=" + userAge +
"}";
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
Test方法:
@org.junit.Test
public void jsonTest() {
JsonTestBean jsonTestBean = new JsonTestBean();
jsonTestBean.setUserName("username1");
jsonTestBean.setExpValue("exp1");
jsonTestBean.setUserAge(11);
System.out.println("bean->json : " + JsonUtils.toJson(jsonTestBean));
String jsonStr = "{\"exp_value\":\"exp2\",\"user_age\":22,\"user_name\":\"username2\"}";
JsonTestBean toBean = JsonUtils.toBean(jsonStr, JsonTestBean.class);
if (toBean != null) {
System.out.println("json-> bean : " + toBean.toString());
執行結果:
bean->json : {"exp_value":"exp1","user_age":11,"user_name":"username1"}
json-> bean : JsonTestBean{expValue="exp2", userName="username2", userAge=22}
可以發現@JSONField是可以放在屬性上面的
專案中遇到後臺傳給前臺的json字串命名方式不符合java的命名規範如:{"exp_value":"exp1","user_age":11,"user_name":"username1"}javabean中是小駝峰命名:
private String userName;
private String expValue;
private int userAge
關於FastJson屬性別名的問題,網上很多文章都指出@JSONField(name = "***")必須要註解在setter和getter上面;但是實際測試compile "com.alibaba:fastjson:1.1.56"版本的FastJson發現@JSONField註解其實可以直接寫到屬性名上面,例項如下:
JavaBean
public class JsonTestBean {
@JSONField(name = "user_name")
private String userName;
@JSONField(name = "exp_value")
private String expValue;
@JSONField(name = "user_age")
private int userAge;
public String getExpValue() {
return expValue;
}
public void setExpValue(String expValue) {
this.expValue = expValue;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "JsonTestBean{" +
"expValue="" + expValue + "\"" +
", userName="" + userName + "\"" +
", userAge=" + userAge +
"}";
}
}
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
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
Test方法:
@org.junit.Test
public void jsonTest() {
JsonTestBean jsonTestBean = new JsonTestBean();
jsonTestBean.setUserName("username1");
jsonTestBean.setExpValue("exp1");
jsonTestBean.setUserAge(11);
System.out.println("bean->json : " + JsonUtils.toJson(jsonTestBean));
String jsonStr = "{\"exp_value\":\"exp2\",\"user_age\":22,\"user_name\":\"username2\"}";
JsonTestBean toBean = JsonUtils.toBean(jsonStr, JsonTestBean.class);
if (toBean != null) {
System.out.println("json-> bean : " + toBean.toString());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
執行結果:
bean->json : {"exp_value":"exp1","user_age":11,"user_name":"username1"}
json-> bean : JsonTestBean{expValue="exp2", userName="username2", userAge=22}
可以發現@JSONField是可以放在屬性上面的