1. 程式人生 > >Jackson將json字符串轉換成泛型List

Jackson將json字符串轉換成泛型List

str json數組 std cer time stl cond exc obj

情景:

需求,需要做一個接口,請求體中的參數是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