简体   繁体   English

无法使用 Jackson 解析 JSON(映射不起作用)

[英]Unable to parse JSON with Jackson (mapping doesn't work)

I am trying to use Jackson to parse sample json as demonstrated below.我正在尝试使用 Jackson 来解析示例 json,如下所示。 However, I the parsing doesn't work (fails without any exceptions - as I get an empty string for event.getAccountId(); What could I be doing wrong? Thanks!但是,我的解析不起作用(无任何例外地失败 - 因为我得到一个空字符串 event.getAccountId(); 我做错了什么?谢谢!

        ObjectMapper om = new ObjectMapper();
    String json = "{\"_procurementEvent\" : [{ \"accountId\" : \"3243234\",\"procurementType\" : \"view\"," +
            "\"_procurementSubType\" : \"Standard Connector\",\"_quantity\" : \"4\", \"_pricePerMonth\" : \"100.00\"" +
            ",\"_annualPrice\" : \"1200.00\"}]}";
    ProcurementEvent event = om.readValue(json, ProcurementEvent.class);

    event.getAccountId(); // returns null    

   @JsonIgnoreProperties(ignoreUnknown = true)
    private static class ProcurementEvent {
        private String _accountId;
        private String _procurementType;
        private String _quantity;
        private String _pricePerMonth;
        private String _annualPrice;

        @JsonProperty("accountId")
        public String getAccountId() {
            return _accountId;
        }

        public void setAccountId(String accountId) {
            _accountId = accountId;
        }

        @JsonProperty("procurementType")
        public String getProcurementType() {
            return _procurementType;
        }

        public void setProcurementType(String procurementType) {
            _procurementType = procurementType;
        }

        @JsonProperty("_quantity")
        public String getQuantity() {
            return _quantity;
        }

        public void setQuantity(String quantity) {
            _quantity = quantity;
        }

        @JsonProperty("_pricePerMonth")
        public String getPricePerMonth() {
            return _pricePerMonth;
        }

        public void setPricePerMonth(String pricePerMonth) {
            _pricePerMonth = pricePerMonth;
        }

        @JsonProperty("_annualPrice")
        public String getAnnualPrice() {
            return _annualPrice;
        }

        public void setAnnualPrice(String annualPrice) {
            _annualPrice = annualPrice;
        }
    }

In the question, try the following approach: 在问题中,尝试以下方法:

class ProcurementEvents {
  private List<ProcurementEvent> _procurementEvent; // + annotations like @JsonIgnoreProperties, getters/ setters, etc.
}

// json from your example
ProcurementEvents events = om.readValue(json, ProcurementEvents.class);
events.get(0).getAccountId();

Your code deserializes an instance of ProcurementEvent from a json representation. 您的代码从json表示中反序列化ProcurementEvent实例。 The problem there is that your json represents a mapping string=>list of object representation . 那里的问题是您的json表示string=>list of object representation形式的映射string=>list of object representation You can have the code work without changes but you need to edit the Json data; 您可以使代码工作不变,但是您需要编辑Json数据。 the example will work when the json contains the object representation only. 当json仅包含object representation时,该示例将起作用。

Modify the json data as: 将json数据修改为:

String json = "{ \"accountId\" : \"3243234\",\"procurementType\" : \"view\","
    + "\"_procurementSubType\" : \"Standard Connector\",\"_quantity\" : \"4\", "
    + "\"_pricePerMonth\" : \"100.00\",\"_annualPrice\" : \"1200.00\"}";

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

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