Skip to content

Commit aac68fa

Browse files
author
371718330@qq.com
committed
增加Json工具类
1 parent ae1a709 commit aac68fa

File tree

1 file changed

+315
-0
lines changed
  • CommonUtil/src/main/java/com/jingewenku/abrahamcaijin/commonutil

1 file changed

+315
-0
lines changed
Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
package com.jingewenku.abrahamcaijin.commonutil;
2+
3+
import com.google.gson.Gson;
4+
import org.json.JSONArray;
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
import org.json.JSONTokener;
8+
9+
import java.lang.reflect.Array;
10+
import java.math.BigDecimal;
11+
import java.math.BigInteger;
12+
import java.util.Collection;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Set;
16+
17+
/**
18+
* @Description:主要功能:Json工具类(需要依赖Gson 2.0以上)
19+
* @Prject: CommonUtilLibrary
20+
* @Package: com.jingewenku.abrahamcaijin.commonutil
21+
* @author: AbrahamCaiJin
22+
* @date: 2017年06月06日 16:16
23+
* @Copyright: 个人版权所有
24+
* @Company:
25+
* @version: 1.0.0
26+
*/
27+
28+
public class JsonUtils {
29+
30+
/**
31+
* 对象转json
32+
* @param obj
33+
* @return
34+
*/
35+
public static String toJson(Object obj) {
36+
Gson gson = new Gson();
37+
return gson.toJson(obj);
38+
}
39+
40+
/**
41+
* json转对象
42+
* @param str
43+
* @param type
44+
* @param <T>
45+
* @return
46+
*/
47+
public static <T> T fromJson(String str, Class<T> type) {
48+
Gson gson = new Gson();
49+
return gson.fromJson(str, type);
50+
}
51+
52+
/**
53+
* Map转为JSONObject
54+
* @param data
55+
* @return
56+
*/
57+
public static JSONObject map2Json(Map<?, ?> data) {
58+
JSONObject object = new JSONObject();
59+
60+
for (Map.Entry<?, ?> entry : data.entrySet()) {
61+
String key = (String) entry.getKey();
62+
if (key == null) {
63+
throw new NullPointerException("key == null");
64+
}
65+
try {
66+
object.put(key, wrap(entry.getValue()));
67+
} catch (JSONException e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
72+
return object;
73+
}
74+
75+
/**
76+
* 集合转换为JSONArray
77+
* @param data
78+
* @return
79+
*/
80+
public static JSONArray collection2Json(Collection<?> data) {
81+
JSONArray jsonArray = new JSONArray();
82+
if (data != null) {
83+
for (Object aData : data) {
84+
jsonArray.put(wrap(aData));
85+
}
86+
}
87+
return jsonArray;
88+
}
89+
90+
/**
91+
* Object对象转换为JSONArray
92+
* @param data
93+
* @return
94+
* @throws JSONException
95+
*/
96+
public static JSONArray object2Json(Object data) throws JSONException {
97+
if (!data.getClass().isArray()) {
98+
throw new JSONException("Not a primitive data: " + data.getClass());
99+
}
100+
final int length = Array.getLength(data);
101+
JSONArray jsonArray = new JSONArray();
102+
for (int i = 0; i < length; ++i) {
103+
jsonArray.put(wrap(Array.get(data, i)));
104+
}
105+
106+
return jsonArray;
107+
}
108+
109+
private static Object wrap(Object o) {
110+
if (o == null) {
111+
return null;
112+
}
113+
if (o instanceof JSONArray || o instanceof JSONObject) {
114+
return o;
115+
}
116+
try {
117+
if (o instanceof Collection) {
118+
return collection2Json((Collection<?>) o);
119+
} else if (o.getClass().isArray()) {
120+
return object2Json(o);
121+
}
122+
if (o instanceof Map) {
123+
return map2Json((Map<?, ?>) o);
124+
}
125+
126+
if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long
127+
|| o instanceof Short || o instanceof String) {
128+
return o;
129+
}
130+
if (o.getClass().getPackage().getName().startsWith("java.")) {
131+
return o.toString();
132+
}
133+
} catch (Exception ignored) {
134+
}
135+
return null;
136+
}
137+
138+
/**
139+
* json字符串生成JSONObject对象
140+
* @param json
141+
* @return
142+
*/
143+
public static JSONObject string2JSONObject(String json) {
144+
JSONObject jsonObject = null;
145+
try {
146+
JSONTokener jsonParser = new JSONTokener(json);
147+
jsonObject = (JSONObject) jsonParser.nextValue();
148+
} catch (Exception e) {
149+
e.printStackTrace();
150+
}
151+
return jsonObject;
152+
}
153+
154+
/**
155+
* 对象转换为Json
156+
* @param obj
157+
* @return
158+
*/
159+
public static String object2json(Object obj) {
160+
StringBuilder json = new StringBuilder();
161+
if (obj == null) {
162+
json.append("\"\"");
163+
} else if (obj instanceof String || obj instanceof Integer
164+
|| obj instanceof Float || obj instanceof Boolean
165+
|| obj instanceof Short || obj instanceof Double
166+
|| obj instanceof Long || obj instanceof BigDecimal
167+
|| obj instanceof BigInteger || obj instanceof Byte) {
168+
json.append("\"").append(string2json(obj.toString())).append("\"");
169+
} else if (obj instanceof Object[]) {
170+
json.append(array2json((Object[]) obj));
171+
} else if (obj instanceof List) {
172+
json.append(list2json((List<?>) obj));
173+
} else if (obj instanceof Map) {
174+
json.append(map2json((Map<?, ?>) obj));
175+
} else if (obj instanceof Set) {
176+
json.append(set2json((Set<?>) obj));
177+
}
178+
return json.toString();
179+
}
180+
181+
/**
182+
* List集合转换为Json
183+
* @param list
184+
* @return
185+
*/
186+
public static String list2json(List<?> list) {
187+
StringBuilder json = new StringBuilder();
188+
json.append("[");
189+
if (list != null && list.size() > 0) {
190+
for (Object obj : list) {
191+
json.append(object2json(obj));
192+
json.append(",");
193+
}
194+
json.setCharAt(json.length() - 1, ']');
195+
} else {
196+
json.append("]");
197+
}
198+
return json.toString();
199+
}
200+
201+
/**
202+
* 对象数组转换为Json
203+
* @param array
204+
* @return
205+
*/
206+
public static String array2json(Object[] array) {
207+
StringBuilder json = new StringBuilder();
208+
json.append("[");
209+
if (array != null && array.length > 0) {
210+
for (Object obj : array) {
211+
json.append(object2json(obj));
212+
json.append(",");
213+
}
214+
json.setCharAt(json.length() - 1, ']');
215+
} else {
216+
json.append("]");
217+
}
218+
return json.toString();
219+
}
220+
221+
/**
222+
* Map集合转换为Json
223+
* @param map
224+
* @return
225+
*/
226+
public static String map2json(Map<?, ?> map) {
227+
StringBuilder json = new StringBuilder();
228+
json.append("{");
229+
if (map != null && map.size() > 0) {
230+
for (Object key : map.keySet()) {
231+
json.append(object2json(key));
232+
json.append(":");
233+
json.append(object2json(map.get(key)));
234+
json.append(",");
235+
}
236+
json.setCharAt(json.length() - 1, '}');
237+
} else {
238+
json.append("}");
239+
}
240+
return json.toString();
241+
}
242+
243+
/**
244+
* Set集合转为Json
245+
* @param set
246+
* @return
247+
*/
248+
public static String set2json(Set<?> set) {
249+
StringBuilder json = new StringBuilder();
250+
json.append("[");
251+
if (set != null && set.size() > 0) {
252+
for (Object obj : set) {
253+
json.append(object2json(obj));
254+
json.append(",");
255+
}
256+
json.setCharAt(json.length() - 1, ']');
257+
} else {
258+
json.append("]");
259+
}
260+
return json.toString();
261+
}
262+
263+
/**
264+
* 字符串转换为Json
265+
* @param s
266+
* @return
267+
*/
268+
public static String string2json(String s) {
269+
if (s == null)
270+
return "";
271+
StringBuilder sb = new StringBuilder();
272+
for (int i = 0; i < s.length(); i++) {
273+
char ch = s.charAt(i);
274+
switch (ch) {
275+
case '"':
276+
sb.append("\\\"");
277+
break;
278+
case '\\':
279+
sb.append("\\\\");
280+
break;
281+
case '\b':
282+
sb.append("\\b");
283+
break;
284+
case '\f':
285+
sb.append("\\f");
286+
break;
287+
case '\n':
288+
sb.append("\\n");
289+
break;
290+
case '\r':
291+
sb.append("\\r");
292+
break;
293+
case '\t':
294+
sb.append("\\t");
295+
break;
296+
case '/':
297+
sb.append("\\/");
298+
break;
299+
default:
300+
if (ch >= '\u0000' && ch <= '\u001F') {
301+
String ss = Integer.toHexString(ch);
302+
sb.append("\\u");
303+
for (int k = 0; k < 4 - ss.length(); k++) {
304+
sb.append('0');
305+
}
306+
sb.append(ss.toUpperCase());
307+
} else {
308+
sb.append(ch);
309+
}
310+
}
311+
}
312+
return sb.toString();
313+
}
314+
315+
}

0 commit comments

Comments
 (0)