Jackson JsonMappingException无法立即实现

时间:2016-03-22 10:35:39

标签: java android json jackson retrofit2

我正在使用改装2和杰克逊转换器下载和解析这个json

`{
  "message": "Action complete.",
  "successCode": 0,
  "data": {
    "miscellaneous": {
      "_id": "5678efcbcf5a5dc54f401eb9",
      "supportEmailDetails": {
        "body": "google.com",
        "subject": "google.com",
        "email": "google@gmail.com"
      },
      "supportPhone": "12345667899",
      "tempClickStaffingAgreementURL": "google.com"
    }
  }
}`

但它给了我以下错误

  

com.fasterxml.jackson.databind.JsonMappingException:不合适   为类型[simple type,class。]找到的构造函数   modals.MMiscellaneous $ Miscellaneous]:无法从JSON实例化   对象(缺少默认构造函数或创建者,或者可能需要   添加/启用类型信息?)在[来源:   java.io.InputStreamReader@1b44a0a3; line:1,column:72](通过   参考链:   modals.MMiscellaneous ["数据"] - > modals.Data ["杂项"])

我在http://www.jsonschema2pojo.org/的帮助下生成了pojo并创建了一个文件,所有生成的类都是封闭的

但是当我在// https://timboudreau.com/blog/json/read网站的帮助下生成pojo类时,它给了我杰克逊的正确结果,

我曾试图实现互联网的每一个结果来解决这个问题但是失败了,所以帮助我解决这个问题

这是我的java代码,

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "message",
    "successCode",
    "data"
})
public class MMiscellaneous {

@JsonProperty("message")
private String message;
@JsonProperty("successCode")
private Integer successCode;
@JsonProperty("data")
private Data data;
@JsonIgnore
private Map<String, Object> additionalProperties = new    HashMap<String, Object>();

/**
 * No args constructor for use in serialization
 *
 */
public MMiscellaneous() {
}

/**
 *
 * @param message
 * @param successCode
 * @param data
 */
public MMiscellaneous(String message, Integer successCode, Data data) {
    this.message = message;
    this.successCode = successCode;
    this.data = data;
}

/**
 *
 * @return
 * The message
 */
@JsonProperty("message")
public String getMessage() {
    return message;
}

/**
 *
 * @param message
 * The message
 */
@JsonProperty("message")
public void setMessage(String message) {
    this.message = message;
}

/**
 *
 * @return
 * The successCode
 */
@JsonProperty("successCode")
public Integer getSuccessCode() {
    return successCode;
}

/**
 *
 * @param successCode
 * The successCode
 */
@JsonProperty("successCode")
public void setSuccessCode(Integer successCode) {
    this.successCode = successCode;
}

/**
 *
 * @return
 * The data
 */
@JsonProperty("data")
public Data getData() {
    return data;
}

/**
 *
 * @param data
 * The data
 */
@JsonProperty("data")
public void setData(Data data) {
    this.data = data;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "miscellaneous"
})
public class Data {

    @JsonProperty("miscellaneous")
    private Miscellaneous miscellaneous;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     *
     */
    public Data() {
    }

    /**
     *
     * @param miscellaneous
     */
    public Data(Miscellaneous miscellaneous) {
        this.miscellaneous = miscellaneous;
    }

    /**
     *
     * @return
     * The miscellaneous
     */
    @JsonProperty("miscellaneous")
    public Miscellaneous getMiscellaneous() {
        return miscellaneous;
    }

    /**
     *
     * @param miscellaneous
     * The miscellaneous
     */
    @JsonProperty("miscellaneous")
    public void setMiscellaneous(Miscellaneous miscellaneous) {
        this.miscellaneous = miscellaneous;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "_id",
        "supportEmailDetails",
        "supportPhone",
        "tempClickStaffingAgreementURL"
})
public class Miscellaneous {

    @JsonProperty("_id")
    private String Id;
    @JsonProperty("supportEmailDetails")
    private SupportEmailDetails supportEmailDetails;
    @JsonProperty("supportPhone")
    private String supportPhone;
    @JsonProperty("tempClickStaffingAgreementURL")
    private String tempClickStaffingAgreementURL;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     *
     */
    public Miscellaneous() {
    }

    /**
     *
     * @param supportPhone
     * @param Id
     * @param tempClickStaffingAgreementURL
     * @param supportEmailDetails
     */
    public Miscellaneous(String Id, SupportEmailDetails supportEmailDetails, String supportPhone, String tempClickStaffingAgreementURL) {
        this.Id = Id;
        this.supportEmailDetails = supportEmailDetails;
        this.supportPhone = supportPhone;
        this.tempClickStaffingAgreementURL = tempClickStaffingAgreementURL;
    }

    /**
     *
     * @return
     * The Id
     */
    @JsonProperty("_id")
    public String getId() {
        return Id;
    }

    /**
     *
     * @param Id
     * The _id
     */
    @JsonProperty("_id")
    public void setId(String Id) {
        this.Id = Id;
    }

    /**
     *
     * @return
     * The supportEmailDetails
     */
    @JsonProperty("supportEmailDetails")
    public SupportEmailDetails getSupportEmailDetails() {
        return supportEmailDetails;
    }

    /**
     *
     * @param supportEmailDetails
     * The supportEmailDetails
     */
    @JsonProperty("supportEmailDetails")
    public void setSupportEmailDetails(SupportEmailDetails supportEmailDetails) {
        this.supportEmailDetails = supportEmailDetails;
    }

    /**
     *
     * @return
     * The supportPhone
     */
    @JsonProperty("supportPhone")
    public String getSupportPhone() {
        return supportPhone;
    }

    /**
     *
     * @param supportPhone
     * The supportPhone
     */
    @JsonProperty("supportPhone")
    public void setSupportPhone(String supportPhone) {
        this.supportPhone = supportPhone;
    }

    /**
     *
     * @return
     * The tempClickStaffingAgreementURL
     */
    @JsonProperty("tempClickStaffingAgreementURL")
    public String getTempClickStaffingAgreementURL() {
        return tempClickStaffingAgreementURL;
    }

    /**
     *
     * @param tempClickStaffingAgreementURL
     * The tempClickStaffingAgreementURL
     */
    @JsonProperty("tempClickStaffingAgreementURL")
    public void setTempClickStaffingAgreementURL(String tempClickStaffingAgreementURL) {
        this.tempClickStaffingAgreementURL = tempClickStaffingAgreementURL;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "body",
        "subject",
        "email"
})
public class SupportEmailDetails {

    @JsonProperty("body")
    private String body;
    @JsonProperty("subject")
    private String subject;
    @JsonProperty("email")
    private String email;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     *
     */
    public SupportEmailDetails() {
    }

    /**
     *
     * @param body
     * @param email
     * @param subject
     */
    public SupportEmailDetails(String body, String subject, String email) {
        this.body = body;
        this.subject = subject;
        this.email = email;
    }

    /**
     *
     * @return
     * The body
     */
    @JsonProperty("body")
    public String getBody() {
        return body;
    }

    /**
     *
     * @param body
     * The body
     */
    @JsonProperty("body")
    public void setBody(String body) {
        this.body = body;
    }

    /**
     *
     * @return
     * The subject
     */
    @JsonProperty("subject")
    public String getSubject() {
        return subject;
    }

    /**
     *
     * @param subject
     * The subject
     */
    @JsonProperty("subject")
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     *
     * @return
     * The email
     */
    @JsonProperty("email")
    public String getEmail() {
        return email;
    }

    /**
     *
     * @param email
     * The email
     */
    @JsonProperty("email")
    public void setEmail(String email) {
        this.email = email;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

}

1 个答案:

答案 0 :(得分:4)

您需要使用公共静态内部类,否则在没有外部类的实例的情况下无法创建新实例。

public static class Miscellaneous {
...
}