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

时间:2022-11-03 09:49:07
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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);
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/moneyshi/article/details/44852343