码迷,mamicode.com
首页 > Web开发 > 详细

Jackson将json字符串转换成泛型List

时间:2017-12-04 23:43:04      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:method   logger   ica   const   三级   response   deb   tle   level   

 

情景:

需求,需要做一个接口,请求体中的参数是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

标签:method   logger   ica   const   三级   response   deb   tle   level   

原文地址:http://www.cnblogs.com/huangtao1927/p/7979394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!