我试图用一个简单的动态变量来反序列化json:在嵌套的" Lottery"上,最后一个有时没有来,有时是空的:
{
"name": "Michael ",
"surname":"Rodriguez",
"Lottery": {
"number 1": "855-333-4432",
"number 2": "815-355-1111",
"number 3": "208-222-322"
}
},
{
"name": "Captain ",
"surname":"America",
"Lottery": {
"number 1": "444-333-888",
"number 2": "222-333-444",
}
},
{
"name": "Pet ",
"surname":"Sounds",
"Lottery": {
"number 1": "815-833-8888",
"number 2": "834-343-1343",
"number 3": ""
}
}
所以我要创建一个这样的模型类:
public class Person {
private String name;
private String surname;
private Lottery Lottery;
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public Lottery getLottery() {
return Lottery;
}
public void setName(String sName) {
name = sName;
}
public void setSurame(String sSurname) {
surname = sSurname;
}
public void setLottery(Lottery sLottery) {
Lottery = sLottery;
}
}
public class Lottery {
private String number 1;
private String number 2;
private String number 3;
public String getnumber 1() {
return number 1;
}
public String getnumber 2() {
return number 2;
}
public String getnumber 3() {
return number 3;
}
public void setnumber 1(String snumber 1) {
number 1 = snumber 1;
}
public void setnumber 2(String snumber 2) {
number 2 = snumber 2;
}
public void setnumber 3(String snumber 3) {
number 3 = snumber 3;
}
}
我做得对,或者我对丢失的变量有疑问吗?
提前致谢!!!!
答案 0 :(得分:1)
如果您使用与字段相同的名称,则不需要将@JsonProperty添加到每个字段,因为默认情况下已完成。杰克逊可以毫无问题地处理空值。