使用Jackson Parser时出现默认构造函数问题

时间:2015-01-19 10:10:50

标签: android android-studio jackson android-gradle

我的模型getter setter类看起来像这样: -

@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
    public CuratedOffers() {
    }

    @JsonProperty("response")
    private String response;
    public String getResponse() {
        return response;
    }
    public void setResponse(String response) {
        this.response = response;
    }

    @JsonProperty("data")
    private Data mData;
    public Data getmData() {
        return mData;
    }
    public void setmData(Data mData) {
        this.mData = mData;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Data{


        @JsonProperty("vendors")
        private List<Vendor> vendorList;
        public List<Vendor> getVendorList() {
            return vendorList;
        }
        public void setVendorList(List<Vendor> vendorList) {
            this.vendorList = vendorList;
        }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Vendor {



        @JsonProperty("id")
        private String Id;
        public String getId() {
            return Id;
        }
        public void setId(String id) {
            Id = id;
        }



        @JsonProperty("name")
        private String venName;
        public String getVenName() {
            return venName;
        }
        public void setVenName(String venName) {
            this.venName = venName;
        }

        @JsonProperty("image")
        private String image;
        public String getImage() {
            return image;
        }
        public void setImage(String image) {
            this.image = image;
        }

        @JsonProperty("logo")
        private String logo;
        public String getLogo() {
            return logo;
        }
        public void setLogo(String logo) {
            this.logo = logo;
        }


        @JsonProperty("offers")
        private String offers;
        public String getOffers() {
            return offers;
        }
        public void setOffers(String offers) {
            this.offers = offers;
        }

        @JsonProperty("description")
        private String offer_description;
        public void setOffer_description(String offer_description) {
            this.offer_description = offer_description;
        }
        public String getOffer_description() {
            return offer_description;
        }
    }
}

我在通过gradle编译时使用Jackson,即: -

compile 'com.fasterxml.jackson.core:jackson-databind:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.2'

编译后我在堆栈跟踪中不断收到此错误

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.yoy.CuratedOffers$Data]: can not instantiate from JSON object (need to add/enable type information?)

搜索SO没有任何合适的答案。帮助将不胜感激!!

1 个答案:

答案 0 :(得分:1)

正如@ vilpel89所建议的,我忘了在CuratedOffers类中声明一个静态嵌套类。现在我的更新类是: -

@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratedOffers {
    public CuratedOffers() {
    }

    @JsonProperty("response")
    private String response;
    public String getResponse() {
        return response;
    }
    public void setResponse(String response) {
        this.response = response;
    }

    @JsonProperty("data")
    private Data mData;
    public Data getmData() {
        return mData;
    }
    public void setmData(Data mData) {
        this.mData = mData;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Data{
        public Data() {
        }

        @JsonProperty("vendors")
        private List<Vendor> vendorList;
        public List<Vendor> getVendorList() {
            return vendorList;
        }
        public void setVendorList(List<Vendor> vendorList) {
            this.vendorList = vendorList;
        }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Vendor {

        public Vendor() {
        }

        @JsonProperty("id")
        private String Id;
        public String getId() {
            return Id;
        }
        public void setId(String id) {
            Id = id;
        }



        @JsonProperty("name")
        private String venName;
        public String getVenName() {
            return venName;
        }
        public void setVenName(String venName) {
            this.venName = venName;
        }

        @JsonProperty("image")
        private String image;
        public String getImage() {
            return image;
        }
        public void setImage(String image) {
            this.image = image;
        }

        @JsonProperty("logo")
        private String logo;
        public String getLogo() {
            return logo;
        }
        public void setLogo(String logo) {
            this.logo = logo;
        }


        @JsonProperty("offers")
        private String offers;
        public String getOffers() {
            return offers;
        }
        public void setOffers(String offers) {
            this.offers = offers;
        }

        @JsonProperty("description")
        private String offer_description;
        public void setOffer_description(String offer_description) {
            this.offer_description = offer_description;
        }
        public String getOffer_description() {
            return offer_description;
        }
    }
}

在我的Data类和Vendor Class中添加了一个静态构造函数,并为它添加了默认构造函数。现在它的工作方式就像一个魅力!希望有一天它可以帮助某人!