有没有办法将嵌套的json转换为unnested对象 我从Web服务中获得了嵌套的json
{
name:"name"
address:{addressName:"address",
addressList:[{State:"state1",
City:"city1"},
{State:"state2",
City:"city2"}]
}
}
我的域名就像
public class Person{
private String name;
private String addressName;
private String addressState1;
private String addressCity1;
private String addressState2;
private String addressCity2;
}
因为只有两个地址将由网络服务提供,所以我们不需要创建另一个域作为PersonAddress,是否有任何注释我可以使用功能 @JsonUnwrapped < / strong>,所以我把json投入了我的域名。
答案 0 :(得分:1)
我猜你可以使用@JsonCreator和@JsonProperty构建一个Person的新实例,但我担心你还需要一个单独的PersonAddress和AddressDetails域类:
PersonAddress:
public class PersonAddress {
private String addressName;
private List<AddressDetails> addressList;
}
AddressDetails:
public class AddressDetails {
private String state;
private String city;
}
人:
public class Person {
private String name;
private String addressName;
private String addressState1;
private String addressCity1;
private String addressState2;
private String addressCity2;
@JsonCreator
public Person(@JsonProperty("name") String name, @JsonProperty("address") PersonAddress address) {
this.addressName = address.addressName;
this.addressState1 = address.addressList.get(0).getState();
this.addressCity1 = address.addressList.get(0).getCity();
this.addressState2 = address.addressList.get(1).getState();
this.addressCity2 = address.addressList.get(1).getCity();
}
}