最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java的Jackson库中复杂对象集合的几种简单转换
时间:2017-03-23 编辑:简简单单 来源:一聚教程网
话不多说,请看代码:
代码如下 | 复制代码 |
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 复杂 对象集合 的几种简单转换 * @author lenovo * * @param */ publicclassMain { staticObjectMapper mapper =newObjectMapper(); publicstaticvoidmain(String[] args)throwsJsonParseException, JsonMappingException, IOException { String josn ="{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}"; User u = mapper.readValue(josn, User.class); // User u=new Main System.out.println("转对象:"+ u); // 转集合 String josn2 ="[{\"UserID\":1,\"LoginName\":\"唐工\",\"Truename\":\"超级\",\"Nickname\":null,\"LoginPwd\":\"E10ADC3949BA59ABBE56E057F20F883E\",\"QQ\":\"\",\"Phone\":\"\",\"Email\":null,\"Remark\":\"\",\"Account_Non_Locked\":0,\"Telelephone\":null,\"IsDelete\":0}]"; JavaType javaType = mapper.getTypeFactory().constructParametricType( List.class, User.class); List System.out.println("转集合me:"+ me); // 对象里有 集合 转换 String josn3 ="{\"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}]}"; User u3 = mapper.readValue(josn3, User.class);// 简单方式 // User u3=new Main System.out.println("转对象里有集合u3:"+ u3); // 集合 对象 集合 转换 String josn4 ="[{\"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}]}]"; JavaType javaType4 = mapper.getTypeFactory().constructParametricType( List.class, User.class); List System.out.println("集合里是对象 对象里有集合转换:"+ list); } /*** * 转对象 * @param josn * @param clz * @return */ publicT jsonStreamConverObject(String josn, Class { T t =null; // ObjectMapper jacksonMapper = new ObjectMapper(); InputStreamReader in =newInputStreamReader(newByteArrayInputStream( josn.getBytes())); BufferedReader streamReader =newBufferedReader(in); StringBuilder buff =newStringBuilder(); String inputStr; try { while((inputStr = streamReader.readLine()) !=null) buff.append(inputStr); // ObjectMapper mapper = new ObjectMapper(); t = mapper.readValue(buff.toString(), clz); }catch(IOException e) { e.printStackTrace(); } returnt; } /*** * 转对象 * @param josn * @param clz * @return */ publicT jsonConverObject(String josn, Class { T t =null; try { t = mapper.readValue(josn, clz); }catch(JsonParseException e) { e.printStackTrace(); }catch(JsonMappingException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } returnt; } /** * 转集合 * @param josn * @param clz * @return */ publicList { List 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); JavaType javaType = mapper.getTypeFactory() .constructParametricType(List.class, clz);// clz.selGenType().getClass() me = mapper.readValue(josn, javaType); }catch(JsonParseException e) { e.printStackTrace(); }catch(JsonMappingException e) { e.printStackTrace(); }catch(IOException e) { 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]]]] * */ |
相关文章
- cad转pdf简单技巧 06-15
- PDF转CAD简单实用的方法 06-13
- PS如何制作转弯金属导线 PS简单制作金属导线教程 04-20
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24