java jsonproperty_java – @JsonProperty Json对象里面的Json对象

如何使用@JsonProperty()在另一个json对象中获取json对象?我想得到的例子是:

"location" : {

"needs_recoding" : false,

"longitude" : "-94.35281245682333",

"latitude" : "35.35363522126198",

"human_address" : "{\"address\":\"7301 ROGERS AVE\",\"city\":\"FORT SMITH\",\"state\":\"AR\",\"zip\":\"\"}"

}

最佳答案 0700提供了在构造函数中使用

@JsonProperty注释的

A helpful reference.一个简单的例子如下所示:

public class Address {

private String address;

private String city;

private String state;

private String zip;

// Constructors, getters/setters

}

public class Location {

private boolean needsRecoding;

private Double longitude;

private Double latitude;

private Address humanAddress;

public Location() {

super();

}

@JsonCreator

public Location(

@JsonProperty("needs_recoding") boolean needsRecoding,

@JsonProperty("longitude") Double longitude,

@JsonProperty("latitude") Double latitude,

@JsonProperty("human_address") Address humanAddress) {

super();

this.needsRecoding = needsRecoding;

this.longitude = longitude;

this.latitude = latitude;

this.humanAddress = humanAddress;

}

// getters/setters

}

或者,您可以将内容直接反序列化为JSON对象树.下面的插图略微修改了Location类示例:

public class Location {

private boolean needsRecoding;

private Double longitude;

private Double latitude;

// Note the use of JsonNode, as opposed to an explicitly created POJO

private JsonNode humanAddress;

public Location() {

super();

}

@JsonCreator

public Location(

@JsonProperty("needs_recoding") boolean needsRecoding,

@JsonProperty("longitude") Double longitude,

@JsonProperty("latitude") Double latitude,

@JsonProperty("human_address") JsonNode humanAddress) {

super();

this.needsRecoding = needsRecoding;

this.longitude = longitude;

this.latitude = latitude;

this.humanAddress = humanAddress;

}

// getters/setters

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值