浅沫记忆 发表于 2021-10-6 18:08:23

Json字符串与Object、List、Map的互转工具类

今天小编就为大家分享一篇关于Json字符串与Object、List、Map的互转工具类,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧


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);
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/moneyshi/article/details/44852343

http://www.zzvips.com/article/172056.html
页: [1]
查看完整版本: Json字符串与Object、List、Map的互转工具类