我试图读取以下JSON字符串到POJO的“属性”下的一些值。但是我得到的只是空值。

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [
      144.9798,
      -37.743
    ]
  },
  "properties": {
    "PFI": "51351644",
    "EZI_ADD": "581 BELL STREET COBURG 3058",
    "ROAD_NAME": "BELL",
    "ROAD_TYPE": "STREET",
    "LOCALITY": "COBURG",
    "LGA_CODE": "316",
    "STATE": "VIC",
    "POSTCODE": "3058",
    "ADD_CLASS": "S"
  },
  "id": "ADDRESS.581"
}


我的POJO课

@JsonIgnoreProperties(ignoreUnknown = true)
class Property {

    public Property(){}

    private String EZI_ADD; // e.g., "14 FAIRWAY COURT BUNDOORA 3083"
    private String STATE; // e.g., "VIC"
    private String POSTCODE; // e.g., "3083"
    private String LGA_CODE; // e.g., 373
    private String LOCALITY; // e.g., "BUNDOORA"
    private String ADD_CLASS; // e.g., "S", or "M"
    private String SA1_7DIG11 = ""; // SA1 code e.g., "2120241"

    public String getEZI_ADD() {
      return EZI_ADD;
    }

    @JsonProperty("EZI_ADD")
    public void setEZI_ADD(String eZI_ADD) {
      EZI_ADD = eZI_ADD;
    }

    public String getSTATE() {
      return STATE;
    }

    @JsonProperty("STATE")
    public void setSTATE(String sTATE) {
      STATE = sTATE;
    }

    public String getPOSTCODE() {
      return POSTCODE;
    }

    @JsonProperty("POSTCODE")
    public void setPOSTCODE(String pOSTCODE) {
      POSTCODE = pOSTCODE;
    }

    public String getLGA_CODE() {
      return LGA_CODE;
    }

    @JsonProperty("LGA_CODE")
    public void setLGA_CODE(String lGA_CODE) {
      LGA_CODE = lGA_CODE;
    }

    public String getLOCALITY() {
      return LOCALITY;
    }

    @JsonProperty("LOCALITY")
    public void setLOCALITY(String lOCALITY) {
      LOCALITY = lOCALITY;
    }

    public String getADD_CLASS() {
      return ADD_CLASS;
    }

    @JsonProperty("ADD_CLASS")
    public void setADD_CLASS(String aDD_CLASS) {
      ADD_CLASS = aDD_CLASS;
    }

    public String getSA1_7DIG11() {
      return SA1_7DIG11;
    }

    @JsonProperty("SA1_7DIG11")
    public void setSA1_7DIG11(String sA1_7DIG11) {
      SA1_7DIG11 = sA1_7DIG11;
    }
}


转换代码如下

//Above json string
String jsonString = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[144.9798,-37.743]},\"properties\":{\"PFI\":\"51351644\",\"EZI_ADD\":\"581 BELL STREET COBURG 3058\",\"ROAD_NAME\":\"BELL\",\"ROAD_TYPE\":\"STREET\",\"LOCALITY\":\"COBURG\",\"LGA_CODE\":\"316\",\"STATE\":\"VIC\",\"POSTCODE\":\"3058\",\"ADD_CLASS\":\"S\"},\"id\":\"ADDRESS.581\"}";
ObjectMapper mapper = new ObjectMapper();
Property properties = mapper.readValue(jsonString, Property.class);


输出:

{
  "properties": {
    "EZI_ADD": null,
    "STATE": null,
    "POSTCODE": null,
    "LGA_CODE": null,
    "LOCALITY": null,
    "ADD_CLASS": null,
    "SA1_7DIG11": ""
  }
}

最佳答案

您发送的JSON字符串与Property类不匹配。添加包装器类,例如像这样的东西:

public class Feature {

    private String type;
    private String id;
    private Property property;

    // getters and setters
}


然后,您可以发送请求,JSON字符串将被解析为您的对象:

{
  "type": "feature",
  "id": "test",
  "property": {
    "PFI": "51351644",
    "EZI_ADD": "581 BELL STREET COBURG 3058",
    "ROAD_NAME": "BELL",
    "ROAD_TYPE": "STREET",
    "LOCALITY": "COBURG",
    "LGA_CODE": "316",
    "STATE": "VIC",
    "POSTCODE": "3058",
    "ADD_CLASS": "S"
  }
}

关于java - 使用Jackson将JSON字符串映射到POJO会给出空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38343573/

10-11 10:38