温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Java的Jackson库中如何实现转换复杂对象集合

发布时间:2020-11-09 17:15:19 来源:亿速云 阅读:402 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关使用Java的Jackson库中如何实现转换复杂对象集合,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

话不多说,请看代码:

package com; 
import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.List; 
import com.fasterxml.jackson.core.JsonParseException; 
import com.fasterxml.jackson.databind.JavaType; 
import com.fasterxml.jackson.databind.JsonMappingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 
/** 
 * jackson 复杂 对象集合 的几种简单转换 
 * @author lenovo 
 * 
 * @param <T> 
 */ 
public class Main<T> 
{ 
 static ObjectMapper mapper = new ObjectMapper(); 
 public static void main(String[] args) throws JsonParseException, 
   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<User>().jsonStreamConverObject(josn, User.class); 
  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<User> me = mapper.readValue(josn2, javaType); 
  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<User>().jsonConverObject(josn3, User.class); 流方式 
  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<User> list = mapper.readValue(josn4, javaType4); 
  System.out.println("集合里是对象 对象里有集合转换:" + list); 
 } 
 /*** 
  * 转对象 
  * @param josn 
  * @param clz 
  * @return 
  */ 
 public T jsonStreamConverObject(String josn, Class<T> clz) 
 { 
  T t = null; 
  // ObjectMapper jacksonMapper = new ObjectMapper(); 
  InputStreamReader in = new InputStreamReader(new ByteArrayInputStream( 
    josn.getBytes())); 
  BufferedReader streamReader = new BufferedReader(in); 
  StringBuilder buff = new StringBuilder(); 
  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(); 
  } 
  return t; 
 } 
 /*** 
  * 转对象 
  * @param josn 
  * @param clz 
  * @return 
  */ 
 public T jsonConverObject(String josn, Class<T> clz) 
 { 
  T t = null; 
  try 
  { 
   t = mapper.readValue(josn, clz); 
  } catch (JsonParseException e) 
  { 
   e.printStackTrace(); 
  } catch (JsonMappingException e) 
  { 
   e.printStackTrace(); 
  } catch (IOException e) 
  { 
   e.printStackTrace(); 
  } 
  return t; 
 } 
 /** 
  * 转集合 
  * @param josn 
  * @param clz 
  * @return 
  */ 
 public List<T> jsonConverList(String josn, 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); 
   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(); 
  } 
  return me; 
 } 
} 
/** 
 * 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]]]] 
 * */ 

看完上述内容,你们对使用Java的Jackson库中如何实现转换复杂对象集合有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI