2017-01-04 43 views
0

的json看起來像下面JsonMappingException:無法反序列化枚舉的例子出來START_OBJECT令牌

{ 
 
name: "math", 
 
code:null, 
 
description:"Mathematics", 
 
id:null, 
 
name:"math", 
 
noExam:null, 
 
teacher:{ 
 
\t id: "57869ced78aa7da0d2ed2d92", 
 
\t courseGroup:"LKG", 
 
\t experties:[{type: "SOCIALSTUDIES", id: "3"}, {type: "PHYSICS", id: "4"}] 
 

 
\t }, 
 
id:"57869ced78aa7da0d2ed2d92" 
 
}

,如果你看到我的實體類,我在Teacher.java一組枚舉

當我嘗試發佈此,我得到錯誤

JsonMappingException: Can not deserialize instance of com.iris.fruits.domain.enumeration.Experties out of START_OBJECT token 

我已經嘗試了幾乎所有的解決方案,如DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,但沒有成功。

public class Subject implements Serializable { 
 
// all the other fields 
 
    @JoinColumn(name = "teacher_id") 
 
    private Teacher teacher; 
 
    
 
    // getter and setter 
 
    } 
 

 

 

 
public class Teacher implements Serializable { 
 
// all the other fields 
 
    
 
    @Id 
 
    @GeneratedValue(strategy = GenerationType.AUTO) 
 
    private String id; 
 

 
    @Enumerated(EnumType.STRING) 
 
    @Column(name = "experties") 
 
    @JsonProperty("experties") 
 
    private List< Experties> experties; 
 

 
    // getter and setter 
 
    } 
 

 

 
    
 
@JsonFormat(shape = JsonFormat.Shape.OBJECT) 
 
public enum Experties implements Serializable { 
 
    MATH(1,"MATH"), 
 
    SCIENCE(2,"SCIENCE"), 
 
    SOCIALSTUDIES(3,"SOCIALSTUDIES"), 
 
    PHYSICS(4,"PHYSICS"), 
 
    CHEMISTRY(5,"CHEMISTRY"); 
 
\t 
 
\t @JsonSerialize(using = ToStringSerializer.class) 
 
\t private String type; 
 
\t 
 
\t @JsonSerialize(using = ToStringSerializer.class) 
 
\t private Integer id; 
 
\t 
 
\t public String getType() { 
 
\t \t return type; 
 
\t } 
 
\t public void setType(String type) { 
 
\t \t this.type = type; 
 
\t } 
 
\t public Integer getId() { 
 
\t \t return id; 
 
\t } 
 
\t public void setId(Integer id) { 
 
\t \t this.id = id; 
 
\t } 
 
\t 
 
\t 
 
\t Experties(Integer id, final String type) { 
 
\t \t this.id = id; \t \t 
 
\t \t this.type = type; 
 
\t } 
 
\t 
 
\t 
 
}

回答

0

你的類應該匹配與JSON的結構。而在你的輸入json不應該重複鍵。

我想你的類,應該下面這樣:

public class Subject implements Serializable { 
// all the other fields 
    String name; 
    String code; 
    String description; 
    String id; 
    String noExam; 
    @JoinColumn(name = "teacher_id") 
    private Teacher teacher; 

    // getter and setter 
    } 



public class Teacher implements Serializable { 
// all the other fields 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private String id; 

    @Enumerated(EnumType.STRING) 
    @Column(name = "experties") 
    @JsonProperty("experties") 
    private List< Experties> experties; 

    String courseGroup; 
    // getter and setter 
    } 



@JsonFormat(shape = JsonFormat.Shape.OBJECT) 
public enum Experties implements Serializable { 
    MATH(1,"MATH"), 
    SCIENCE(2,"SCIENCE"), 
    SOCIALSTUDIES(3,"SOCIALSTUDIES"), 
    PHYSICS(4,"PHYSICS"), 
    CHEMISTRY(5,"CHEMISTRY"); 

    @JsonSerialize(using = ToStringSerializer.class) 
    private String type; 

    @JsonSerialize(using = ToStringSerializer.class) 
    private Integer id; 

    public String getType() { 
     return type; 
    } 
    public void setType(String type) { 
     this.type = type; 
    } 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 


    Experties(Integer id, final String type) { 
     this.id = id;  
     this.type = type; 
    } 


} 
+0

我已經在類中的所有這些領域,只是沒有寫在這裏的空間限制。 – Mayurb

+0

試試這個:ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); ResultOb ob = mapper.readValue(jsonInput,ResultOb.class); – rsh

相關問題