Jackson ObjectMapper的readValue方法无法反序列化json

时间:2016-03-16 09:11:51

标签: java json jackson

我的代码基本上得到一个json字符串,并尝试映射到我有的POJO。它似乎匹配,但我仍然得到映射异常。

这是我的json字符串:

"{"top" : "0px","left" : "1120px","width" : "170px","height" : "190px","invertAxes" : false,"type" : false,"parameters" : null,"parameterFonts" : null,"primary" : null,"secondary" : null}"

这是读取的java代码:

myModel = mapper.readValue(json, MyModel.class);

这是MyModel类:

public class MyModel{
    @JsonProperty("top")
    private String top;

    @JsonProperty("left")
    private String left;

    @JsonProperty("width")
    private String width;

    @JsonProperty("height")
    private String height;

    @JsonProperty("invertAxes")
    private boolean invertAxes;

    @JsonProperty("type")
    private String type;

    @JsonProperty("parameters")
    private String[] parameters;

    @JsonProperty("parameterFonts")
    private String[] parameterFonts;

    @Transient
    private MyInnerModel primary;

    @Transient
    private MyInnerModel secondary;

    public MyModel() {

    }

    public MyModel(String top, String left, String width, String height, boolean invertAxes, String type, String[] parameters, String[] parameterFonts, MyInnerModel primary, MyInnerModel secondary) {
        this.top = top;
        this.left = left;
        this.width = width;
        this.height = height;
        this.invertAxes = invertAxes;
        this.type = type;
        this.parameters = parameters;
        this.parameterFonts = parameterFonts;
        this.primary = primary;
        this.secondary = secondary;
    }

    public String getTop() {
        return top;
    }

    public void setTop(String top) {
        this.top = top;
    }

    public String getLeft() {
        return left;
    }

    public void setLeft(String left) {
        this.left = left;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public boolean isInvertAxes() {
        return invertAxes;
    }

    public void setInvertAxes(boolean invertAxes) {
        this.invertAxes = invertAxes;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String[] getParameters() {
        return parameters;
    }

    public void setParameters(String[] parameters) {
        this.parameters = parameters;
    }

    public String[] getParameterFonts() {
        return parameterFonts;
    }

    public void setParameterFonts(String[] parameterFonts) {
        this.parameterFonts = parameterFonts;
    }

    public MyInnerModel getPrimary() {
        return primary;
    }

    @JsonProperty("primary")
    public void setPrimary(MyInnerModel primary) {
        this.primary = primary;
    }

    public MyInnerModel getSecondary() {
        return secondary;
    }

    @JsonProperty("secondary")
    public void setSecondary(MyInnerModel secondary) {
        this.secondary = secondary;
    }

}

这是我得到的错误:

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.ge.migrate.MyModel] from JSON String; no single-String constructor/factory method

我已多次检查,但无法找到与POJO不匹配的元素。

编辑:这是MyInnerModel:

public class MyInnerModel{

@JsonProperty("yint")
private double yint;

@JsonProperty("ymin")
private double ymin;

@JsonProperty("ymax")
private double ymax;

@JsonProperty("xint")
private double xint;

@JsonProperty("xmin")
private double xmin;

@JsonProperty("xmax")
private double xmax;

@JsonProperty("parameters")
private String [] parameters;

@JsonProperty("xparameter")
private String xParameter;

@JsonProperty("yparameters")
private String [] yParameters;


public MyInnerModel() {

}

public MyInnerModel(double yint, double ymin, double ymax, double xint, double xmin, double xmax, String [] parameters, String xParameter, String [] yParameters) {
    this.yint = yint;
    this.ymin = ymin;
    this.ymax = ymax;
    this.yint = xint;
    this.ymin = xmin;
    this.ymax = xmax;
    this.parameters = parameters;
    this.xParameter = xParameter;
    this.yParameters = yParameters;
}

public double getYint() {
    return yint;
}

public void setYint(double yint) {
    this.yint = yint;
}

public double getYmin() {
    return ymin;
}

public void setYmin(double ymin) {
    this.ymin = ymin;
}

public double getYmax() {
    return ymax;
}

public void setYmax(double ymax) {
    this.ymax = ymax;
}

public String [] getParameters() {
    return parameters;
}

public void setParameters(String [] parameters) {
    this.parameters = parameters;
}

public double getXint() {
    return xint;
}

public void setXint(double xint) {
    this.xint = xint;
}

public double getXmin() {
    return xmin;
}

public void setXmin(int xmin) {
    this.xmin = xmin;
}

public double getXmax() {
    return xmax;
}

public void setXmax(double xmax) {
    this.xmax = xmax;
}

public String getXparameter() {
    return xParameter;
}

public void setXparameter(String xParameter) {
    this.xParameter = xParameter;
}

public String[] getYparameters() {
    return yParameters;
}

public void setYparameters(String[] yParameters) {
    this.yParameters = yParameters;
}

}

1 个答案:

答案 0 :(得分:0)

该错误告诉您没有“没有单字符串构造函数”#39; 如果将构造函数添加到MyInnerModel:

public MyInnerModel(String foo) {

}

它应该有用。

或者从

更改你的json
"primary" : null,"secondary" : null

"primary" : {},"secondary" : {}

空对象和空值在json中不一样。

或更好的是当它们为空时(如果那是一个选项)从json中省略它们