-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonUtils.java
134 lines (115 loc) · 3.13 KB
/
JsonUtils.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.stkj.pperty.util;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.google.common.base.Strings;
/**
*
* @author luoyh
* @date Jul 24, 2015
*/
public abstract class JsonUtils {
public static void main(String[] args) {
List<Map<String, Object>> datas = JsonUtils.readList("[{\"houseNo\":0202030201,\"time\":20171215,\"phone\": 15102351291}]", new TypeReference<List<Map<String, Object>>>() {});
System.out.println(datas);
}
private static final ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
public static <T> T readObject(String jsonStr, Class<T> t) {
if (Strings.isNullOrEmpty(jsonStr))
return null;
try {
return mapper.readValue(jsonStr, t);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static <T> List<T> readList(String jsonStr, TypeReference<List<T>> reference) {
if (Strings.isNullOrEmpty(jsonStr))
return null;
try {
return mapper.readValue(jsonStr, reference);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static Map<String, String> readTree(String jsonStr) {
if (Strings.isNullOrEmpty(jsonStr))
return null;
Map<String, String> map = null;
JsonNode root = null;
try {
root = mapper.readTree(jsonStr);
} catch (IOException e) {
e.printStackTrace();
}
if (root != null) {
map = new HashMap<String, String>();
for (Iterator<String> it = root.fieldNames(); it.hasNext();) {
String field = it.next();
JsonNode node = root.get(field);
JsonNodeType type = node.getNodeType();
if(type == JsonNodeType.NULL) {
map.put(field, null);
} else {
map.put(field, node.asText());
}
}
}
return map;
}
public static void writeObj(OutputStream out, Object obj) {
try {
mapper.writeValue(out, obj);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String writeObjToString(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] writeObjToByte(Object obj) {
try {
return mapper.writeValueAsBytes(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String toStringHex(String s) {
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "utf-8");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
}