将对象转换为JSON错误时:com.fasterxml.jackson.databind.exc.MismatchedInputException

时间:2019-04-16 07:35:44

标签: java json objectmapper

我正在尝试使用List<Object>JSON转换为ObjectMapper格式。我尝试如下所示,但我遇到了异常,即

com.fasterxml.jackson.databind.exc.MismatchedInputException:

    public List<ClientRT> callSP() throws IOException { 

            List<Object> crt=crtRepo.roomtype(60);          

            ObjectMapper mapper = new ObjectMapper();
            String json=mapper.writeValueAsString(crt);

            //ClientRT obj=mapper.readValue(json,ClientRT.class);

            List<ClientRT> obj=mapper.readValue(json,mapper.getTypeFactory()
                                                           .constructCollectionLikeType(List.class,ClientRT.class));
            return obj;
        }

ClientRT类

public class ClientRT { 

    public Integer res_nclient_room_type_id;    
    public String res_sclient_rt_desc;
    public String  res_sclient_rt_name;
    public String res_sclient_rt_code;

    //getter setter

    public ClientRT() {
        super();
    }
    @Override
    public String toString() {
        return "ClientRT [res_nclient_room_type_id=" + res_nclient_room_type_id + ", res_sclient_rt_desc="
                + res_sclient_rt_desc + ", res_sclient_rt_name=" + res_sclient_rt_name + ", res_sclient_rt_code="
                + res_sclient_rt_code + "]";
    }
}

在JSON中预期结果

[  
  { "res_nclient_room_type_id":1 ,   
    "res_sclient_rt_desc": "FACILITIES - AVAILABLE ROOM",
    "res_sclient_rt_name":"FACILITIES - AVAILABLE ROOM" ,
    "res_sclient_rt_code":"050" 
 },

]

例外

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.spacestudy.model.ClientRT` out of START_ARRAY token
 at [Source: (String)"[[1,"TEMPORARILY NOT ASSIGNED","TEMPORARILY NOT ASSIGNED","000"],[2,"FACILITIES - AVAILABLE ROOM","FACILITIES - AVAILABLE ROOM","050"],[3,"FACILITIES - UNDEFINED PURPOSE","FACILITIES - UNDEFINED PURPOSE","060"],[4,"FACILITIES - UNFINISHED AREA","FACILITIES - UNFINISHED AREA","070"],[5,"CLASSROOM/LECTURE HALL","CLASSROOM/LECTURE HALL","110"],[6,"CLASSROOM SERVICE","CLASSROOM SERVICE","115"],[7,"CLASSROOM STORAGE","CLASSROOM STORAGE","116"],[8,"CLASSROOM KITCHEN","CLASSROOM KITCHEN","119"],[9,"CLA"[truncated 8500 chars]; line: 1, column: 2] (through reference chain: java.util.ArrayList[0])

有人可以告诉我如何解决此异常吗?

2 个答案:

答案 0 :(得分:0)

您传递给Jackson的json不正确。

这个Json并没有传递给Jackson:

[  
  { "res_nclient_room_type_id":1 ,   
    "public String res_sclient_rt_desc": "FACILITIES - AVAILABLE ROOM",
    "public String  res_sclient_rt_name":"FACILITIES - AVAILABLE ROOM" ,
    "public String res_sclient_rt_code":"050" 
 }
]

尝试使用此命令(从json中删除public String

[  
  { "res_nclient_room_type_id":1 ,   
    "res_sclient_rt_desc": "FACILITIES - AVAILABLE ROOM",
    "res_sclient_rt_name":"FACILITIES - AVAILABLE ROOM" ,
    "res_sclient_rt_code":"050" 
  }
]

Jackson无法解析json,因为它无法在类ClientRT中找到相应的Java字段。从json中删除public String,仅保留类ClientRT的字段名称。

答案 1 :(得分:0)

如上面的amicoderozer所述,您需要从json而不是从类中删除类NPM的字段的数据类型。