Jackson将json字符串转换成泛型List
Posted 故意养只喵叫顺儿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jackson将json字符串转换成泛型List相关的知识,希望对你有一定的参考价值。
情景:
需求,需要做一个接口,请求体中的参数是string类型。
参数是一个批量的数据,json数组格式,所以需要把string的参数转换成list类型。
参数如下:
[
{
"doctorCode": "0210000",
"hospitalName": "上海市第一人民医院",
"hospitalLevel": "三级甲等",
"hospitalProvince": "上海市",
"firstLevelDepartment": "临床科室",
"secondLevelDepartment": "急诊科",
"doctorName": "张三",
"doctorGender": "男",
"doctorProfessionalTitle": "一级医师",
"doctorCertificateNumber": "111000",
"verificationResult": "0",
"verificationTime": "2017/11/29",
"status": "0",
"remarks1": "备注1",
"remarks2": "备注2",
"remarks3": "备注3"
},
{
"doctorCode": "0210001",
"hospitalName": "上海市第二人民医院",
"hospitalLevel": "三级甲等",
"hospitalProvince": "上海市",
"firstLevelDepartment": "临床科室",
"secondLevelDepartment": "急诊科",
"doctorName": "李四",
"doctorGender": "男",
"doctorProfessionalTitle": "二级医师",
"doctorCertificateNumber": "111001",
"verificationResult": "0",
"verificationTime": "2017/12/4",
"status": "0",
"remarks1": "备注1",
"remarks2": "备注2",
"remarks3": "备注3"
},
{
"doctorCode": "0210001",
"hospitalName": "上海市第二人民医院",
"hospitalLevel": "三级甲等",
"hospitalProvince": "上海市",
"firstLevelDepartment": "临床科室",
"secondLevelDepartment": "急诊科",
"doctorName": "王五",
"doctorGender": "男",
"doctorProfessionalTitle": "二级医师",
"doctorCertificateNumber": "111001",
"verificationResult": "0",
"verificationTime": "2017/12/4",
"status": "0",
"remarks1": "备注1",
"remarks2": "备注2",
"remarks3": "备注3"
},
{
"doctorCode": "0210001",
"hospitalName": "上海市第二人民医院",
"hospitalLevel": "三级甲等",
"hospitalProvince": "上海市",
"firstLevelDepartment": "临床科室",
"secondLevelDepartment": "急诊科",
"doctorName": "赵六",
"doctorGender": "男",
"doctorProfessionalTitle": "二级医师",
"doctorCertificateNumber": "111001",
"verificationResult": "0",
"verificationTime": "2017/12/4",
"status": "0",
"remarks1": "备注1",
"remarks2": "备注2",
"remarks3": "备注3"
}
]
=============================================================================
@RequestMapping(method={RequestMethod.POST}, value="/doctorss")
@ResponseBody
public int testDoctorInformation(@RequestBody String doctor) throws Exception{
logger.debug(DEBUG_PREFIX+"updateDoctorInformation begin,the doctor is:" + doctor);
logger.info(INFO_PREFIX+"updateDoctorInformation begin.");
int resultCode=1;
try {
ObjectMapper objMapper = new ObjectMapper();
JavaType javaType = getCollectionType(ArrayList.class, DoctorInformation.class);
List<DoctorInformation> list = (List<DoctorInformation>)objMapper.readValue(doctor,javaType );
for(DoctorInformation ll:list){
System.out.println(ll.getDoctorName());
}
logger.debug(DEBUG_PREFIX+"updateDoctorInformation end.");
logger.info(INFO_PREFIX+"updateDoctorInformation end");
} catch (Exception e) {
logger.error(ERROR_PREFIX+"execute updateDoctorInformation occur error,the exception is:"+e);
logger.error("系统异常", e);
resultCode=0;
throw e;
}
return resultCode;
}
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
ObjectMapper mapper = new ObjectMapper();
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
以上是关于Jackson将json字符串转换成泛型List的主要内容,如果未能解决你的问题,请参考以下文章