@JsonProperty Json对象内的Json对象 [英] @JsonProperty Json object inside Json object

查看:407
本文介绍了@JsonProperty Json对象内的Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I use the @JsonProperty() to get a json object within another json object? The example json I want to get is:

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

推荐答案

有关使用 @JsonProperty 注释由 StaxMan 提供.一个简单的示例如下所示:

A helpful reference for using @JsonProperty annotations in a constructor is provided by StaxMan. A simple example shown below:

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类示例的稍加修改为例进行说明:

Alternately, you may deserialize content directly into a JSON object tree. Illustrated below with a slight modification of the Location class example:

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
}

这篇关于@JsonProperty Json对象内的Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆