Json字符串与Object、List、Map的互转工具类
程序员文章站
2024-03-02 08:48:40
package com.cq2022.zago.base.util;
import java.io.filereader;
import java.io.fil...
package com.cq2022.zago.base.util; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.stringwriter; import java.util.list; import java.util.map; import org.codehaus.jackson.jsonfactory; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.jsongenerator; import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.map.serializationconfig; import org.codehaus.jackson.map.annotate.jsonserialize; import com.alibaba.fastjson.jsonarray; /** * json工具类,实现json与java bean的互相转换 */ public class jsonutils { private static objectmapper objectmapper = new objectmapper(); private static jsonfactory jsonfactory = new jsonfactory(); static { objectmapper.configure(serializationconfig.feature.write_null_map_values, false); objectmapper.setserializationinclusion(jsonserialize.inclusion.non_null); } /** * 泛型返回,json字符串转对象 * @param jsonasstring * @param pojoclass * @return * @throws jsonmappingexception * @throws jsonparseexception * @throws ioexception */ public static <t> t fromjson(string jsonasstring, class<t> pojoclass) throws jsonmappingexception, jsonparseexception, ioexception { return objectmapper.readvalue(jsonasstring, pojoclass); } public static <t> t fromjson(filereader fr, class<t> pojoclass) throws jsonparseexception, ioexception { return objectmapper.readvalue(fr, pojoclass); } /** * object对象转json * @param pojo * @return * @throws jsonmappingexception * @throws jsongenerationexception * @throws ioexception */ public static string tojson(object pojo) throws jsonmappingexception, jsongenerationexception, ioexception { return tojson(pojo, false); } public static string tojson(object pojo, boolean prettyprint) throws jsonmappingexception, jsongenerationexception, ioexception { stringwriter sw = new stringwriter(); jsongenerator jg = jsonfactory.createjsongenerator(sw); if (prettyprint) { jg.usedefaultprettyprinter(); } objectmapper.writevalue(jg, pojo); return sw.tostring(); } public static void tojson(object pojo, filewriter fw, boolean prettyprint) throws jsonmappingexception, jsongenerationexception, ioexception { jsongenerator jg = jsonfactory.createjsongenerator(fw); if (prettyprint) { jg.usedefaultprettyprinter(); } objectmapper.writevalue(jg, pojo); } /** * json字符串转map * @param jsonstr * @return * @throws ioexception */ public static map<string, object> parsemap(string jsonstr) throws ioexception { map<string, object> map = objectmapper.readvalue(jsonstr, map.class); return map; } public static jsonnode parse(string jsonstr) throws ioexception { jsonnode node = null; node = objectmapper.readtree(jsonstr); return node; } public static objectmapper getobjectmapper() { return objectmapper; } /** * json字符串转 list对象 * @param str json字符串 * @param clazz 转换的类型 * @return */ public static <t> list<t> fromlistjson(string str,class<t> clazz){ return jsonarray.parsearray(str, clazz); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
推荐阅读