繁体   English   中英

@JsonProperty Json对象内的Json对象

[英]@JsonProperty Json object inside Json object

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

"location" : {
  "needs_recoding" : false,
  "longitude" : "-94.35281245682333",
  "latitude" : "35.35363522126198",
  "human_address" : "{\"address\":\"7301 ROGERS AVE\",\"city\":\"FORT SMITH\",\"state\":\"AR\",\"zip\":\"\"}"
}

@JsonProperty提供了有关在构造函数中使用@JsonProperty批注的有用参考 一个简单的例子如下所示:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM