Java的Jackson库中复杂对象集合的几种简单转换
话不多说,请看代码:
packagecom;
importjava.io.BufferedReader;
importjava.io.ByteArrayInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.List;
importcom.fasterxml.jackson.core.JsonParseException;
importcom.fasterxml.jackson.databind.JavaType;
importcom.fasterxml.jackson.databind.JsonMappingException;
importcom.fasterxml.jackson.databind.ObjectMapper;
/**
*jackson复杂对象集合的几种简单转换
*@authorlenovo
*
*@param<T>
*/
publicclassMain<T>
{
staticObjectMappermapper=newObjectMapper();
publicstaticvoidmain(String[]args)throwsJsonParseException,
JsonMappingException,IOException
{
Stringjosn="{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}";
Useru=mapper.readValue(josn,User.class);
//Useru=newMain<User>().jsonStreamConverObject(josn,User.class);
System.out.println("转对象:"+u);
//转集合
Stringjosn2="[{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}]";
JavaTypejavaType=mapper.getTypeFactory().constructParametricType(
List.class,User.class);
List<User>me=mapper.readValue(josn2,javaType);
System.out.println("转集合me:"+me);
//对象里有集合转换
Stringjosn3="{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\"超级管理员\",\"Show_Name\":\"超级管理员\",\"Remark\":null,\"Type\":1}]}";
Useru3=mapper.readValue(josn3,User.class);//简单方式
//Useru3=newMain<User>().jsonConverObject(josn3,User.class);流方式
System.out.println("转对象里有集合u3:"+u3);
//集合对象集合转换
Stringjosn4="[{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\"超级管理员\",\"Show_Name\":\"超级管理员\",\"Remark\":null,\"Type\":1}]},{\"UserID\":2,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0,\"RoleList\":[{\"Roleid\":0,\"Name\":\"超级管理员\",\"Show_Name\":\"超级管理员\",\"Remark\":null,\"Type\":1}]}]";
JavaTypejavaType4=mapper.getTypeFactory().constructParametricType(
List.class,User.class);
List<User>list=mapper.readValue(josn4,javaType4);
System.out.println("集合里是对象对象里有集合转换:"+list);
}
/***
*转对象
*@paramjosn
*@paramclz
*@return
*/
publicTjsonStreamConverObject(Stringjosn,Class<T>clz)
{
Tt=null;
//ObjectMapperjacksonMapper=newObjectMapper();
InputStreamReaderin=newInputStreamReader(newByteArrayInputStream(
josn.getBytes()));
BufferedReaderstreamReader=newBufferedReader(in);
StringBuilderbuff=newStringBuilder();
StringinputStr;
try
{
while((inputStr=streamReader.readLine())!=null)
buff.append(inputStr);
//ObjectMappermapper=newObjectMapper();
t=mapper.readValue(buff.toString(),clz);
}catch(IOExceptione)
{
e.printStackTrace();
}
returnt;
}
/***
*转对象
*@paramjosn
*@paramclz
*@return
*/
publicTjsonConverObject(Stringjosn,Class<T>clz)
{
Tt=null;
try
{
t=mapper.readValue(josn,clz);
}catch(JsonParseExceptione)
{
e.printStackTrace();
}catch(JsonMappingExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}
returnt;
}
/**
*转集合
*@paramjosn
*@paramclz
*@return
*/
publicList<T>jsonConverList(Stringjosn,Class<T>clz)
{
List<T>me=null;
try
{
//jacksonMapper
//.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
//jacksonMapper.enableDefaultTyping();
//jacksonMapper.setVisibility(JsonMethod.FIELD,JsonAutoDetect.Visibility.ANY);
//jacksonMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT,
//false);//格式化
//jacksonMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
//jacksonMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
//false);
JavaTypejavaType=mapper.getTypeFactory()
.constructParametricType(List.class,clz);//clz.selGenType().getClass()
me=mapper.readValue(josn,javaType);
}catch(JsonParseExceptione)
{
e.printStackTrace();
}catch(JsonMappingExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}
returnme;
}
}
/**
*output:
*转对象:User[UserID=1,LoginName=唐工,Truename=超级,Nickname=null,LoginPwd=E10ADC3949BA59ABBE56E057F20F883E,QQ=,Phone=,Email=null,Remark=,Account_Non_Locked=0,Telelephone=null,Indate=null,IsDelete=0,RoleList=null]
*转集合me:[User[UserID=1,LoginName=唐工,Truename=超级,Nickname=null,LoginPwd=E10ADC3949BA59ABBE56E057F20F883E,QQ=,Phone=,Email=null,Remark=,Account_Non_Locked=0,Telelephone=null,Indate=null,IsDelete=0,RoleList=null]]
*转对象里有集合u3:User[UserID=1,LoginName=唐工,Truename=超级,Nickname=null,LoginPwd=E10ADC3949BA59ABBE56E057F20F883E,QQ=,Phone=,Email=null,Remark=,Account_Non_Locked=0,Telelephone=null,Indate=null,IsDelete=0,RoleList=[Role[Roleid=0,Name=超级管理员,Show_Name=超级管理员,Remark=null,Type=1]]]
*集合里是对象对象里有集合转换:[User[UserID=1,LoginName=唐工,Truename=超级,Nickname=null,LoginPwd=E10ADC3949BA59ABBE56E057F20F883E,QQ=,Phone=,Email=null,Remark=,Account_Non_Locked=0,Telelephone=null,Indate=null,IsDelete=0,RoleList=[Role[Roleid=0,Name=超级管理员,Show_Name=超级管理员,Remark=null,Type=1]]],User[UserID=2,LoginName=唐工,Truename=超级,Nickname=null,LoginPwd=E10ADC3949BA59ABBE56E057F20F883E,QQ=,Phone=,Email=null,Remark=,Account_Non_Locked=0,Telelephone=null,Indate=null,IsDelete=0,RoleList=[Role[Roleid=0,Name=超级管理员,Show_Name=超级管理员,Remark=null,Type=1]]]]
**/
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!