Java的Jackson库中复杂对象集合的几种简单转换
程序员文章站
2024-03-08 08:39:21
话不多说,请看代码:
package com;
import java.io.bufferedreader;
import java.io.bytearr...
话不多说,请看代码:
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]]]] * */
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: GPGPU基础(一):一些基本概念