今天就跟大家聊聊有關(guān)使用Java的Jackson庫中如何實現(xiàn)轉(zhuǎn)換復(fù)雜對象集合,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比桑日網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式桑日網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋桑日地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
話不多說,請看代碼:
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 復(fù)雜 對象集合 的幾種簡單轉(zhuǎn)換 * @author lenovo * * @param*/ public class Main { 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 ().jsonStreamConverObject(josn, User.class); System.out.println("轉(zhuǎn)對象:" + u); // 轉(zhuǎn)集合 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 me = mapper.readValue(josn2, javaType); System.out.println("轉(zhuǎn)集合me:" + me); // 對象里有 集合 轉(zhuǎn)換 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 ().jsonConverObject(josn3, User.class); 流方式 System.out.println("轉(zhuǎn)對象里有集合u3:" + u3); // 集合 對象 集合 轉(zhuǎn)換 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 list = mapper.readValue(josn4, javaType4); System.out.println("集合里是對象 對象里有集合轉(zhuǎn)換:" + list); } /*** * 轉(zhuǎn)對象 * @param josn * @param clz * @return */ public T jsonStreamConverObject(String josn, Class 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; } /*** * 轉(zhuǎn)對象 * @param josn * @param clz * @return */ public T jsonConverObject(String josn, Class 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; } /** * 轉(zhuǎn)集合 * @param josn * @param clz * @return */ public List jsonConverList(String josn, Class clz) { List 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: * 轉(zhuǎn)對象: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] * 轉(zhuǎn)集合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]] * 轉(zhuǎn)對象里有集合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]]] * 集合里是對象 對象里有集合轉(zhuǎn)換:[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]]]] * */
看完上述內(nèi)容,你們對使用Java的Jackson庫中如何實現(xiàn)轉(zhuǎn)換復(fù)雜對象集合有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。