I am trying to convert List<Object>
into JSON
format using ObjectMapper
. I tried like below But I am getting exception ie
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 Class
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 + "]";
}
}
Expecting Result In 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"
},
]
Exception
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])
Can any one please tell me how I can resolve this Exception?
The json you are passing to Jackson is incorrect.
Instead of passing to Jackson this Json:
[
{ "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"
}
]
Try with this one (remove public String
from the 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"
}
]
Jackson can't parse the json because it can't find the corresponding Java field in class ClientRT
. Remove public String
from the json and leave only the name of the field of class ClientRT
.
正如上面的amicoderozer所提到的,你需要从json中删除ClientRT
类的字段的数据类型,而不是从类中ClientRT
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.