fastJson在bean中加入@JsonProperty转换出的对象中依然有字段为null的处理
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
1.转换的代码如下
String json = "{\"id\":1059827483,\"idstr\":\"1059827483\",\"class\":1,\"screen_name\":\"DancingToDeath\"}";
System.out.println(json);
U u = JSONObject.parseObject(json, U.class);
System.out.println(u.getId());
System.out.println(u.getIdstr());
System.out.println(u.getMyClass());
System.out.println(u.getScreen());
2.U实体定义如下:
public class U {
private long id;
private String idstr;
@JSONField(name = "class")
private int myClass;
@JsonProperty("screen_name")
private String screen;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getIdstr() {
return idstr;
}
public void setIdstr(String idstr) {
this.idstr = idstr;
}
public int getMyClass() {
return myClass;
}
public void setMyClass(int myClass) {
this.myClass = myClass;
}
public String getScreen() {
return screen;
}
public void setScreen(String screenName) {
this.screen = screenName;
}
}
3.转换结果:
{"id":1059827483,"idstr":"1059827483","class":1,"screen_name":"DancingToDeath"}
1059827483
1059827483
1
null
可见,screen_name并未转换出来,原因在于,对于普通的json解析类,用JsonProperty可以,但是在用fastJson进行解析时,需要用@JSONField(name = "screen_name")来进行标注。
4.改变U
private long id;
private String idstr;
@JSONField(name = "class")
private int myClass;
@JSONField(name = "screen_name")
private String screen;
5.转换结果如下:
1059827483
1059827483
1
DancingToDeath
以此记录,希望能帮助到遇到同样问题的人。
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献5条内容
所有评论(0)