程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java的Jackson庫中復雜對象集合的幾種復雜轉換

Java的Jackson庫中復雜對象集合的幾種復雜轉換

編輯:關於JAVA

Java的Jackson庫中復雜對象集合的幾種復雜轉換。本站提示廣大學習愛好者:(Java的Jackson庫中復雜對象集合的幾種復雜轉換)文章只能為提供參考,不一定能成為您想要的結果。以下是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]]]] 
 * */ 

以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved