Java后台处理Json格式数据的方法
1.将对象转换为JSON字符串,返回值为一个JSON字符串
publicstaticStringtoJson(Objectvalue){
try{
returnmapper.writeValueAsString(value);
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}
2.将JSON字符串转换为实体对象,返回值为实体对象
publicstatic<T>TtoObject(Stringjson,Class<T>valueType){
Assert.hasText(json);
Assert.notNull(valueType);
try{
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
mapper.setDateFormat(dateFormat);
returnmapper.readValue(json,valueType);
}catch(Exceptione){
e.printStackTrace();
}
returnnull;