Json与javaBean之间的转换工具类
/**
* Json与javaBean之间的转换工具类
*
* @author 晚风工作室 www.soservers.com
* @version 20111221
*
* {@code 现使用json-lib组件实现
* 需要
* json-lib-2.4-jdk15.jar
* ezmorph-1.0.6.jar
* commons-collections-3.1.jar
* commons-lang-2.0.jar
* 支持
* }
*/
* Json与javaBean之间的转换工具类
*
* @author 晚风工作室 www.soservers.com
* @version 20111221
*
* {@code 现使用json-lib组件实现
* 需要
* json-lib-2.4-jdk15.jar
* ezmorph-1.0.6.jar
* commons-collections-3.1.jar
* commons-lang-2.0.jar
* 支持
* }
*/
代码片段(5)[全屏查看所有代码]
1. [代码]Json与javaBean之间的转换工具类
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; /** * Json与javaBean之间的转换工具类 * * @author 晚风工作室 www.soservers.com * @version 20111221 * * {@code 现使用json-lib组件实现 * 需要 * json-lib-2.4-jdk15.jar * ezmorph-1.0.6.jar * commons-collections-3.1.jar * commons-lang-2.0.jar * 支持 * } */ public class JsonPluginsUtil { /** * 从一个JSON 对象字符格式中得到一个java对象 * * @param jsonString * @param beanCalss * @return */ @SuppressWarnings ( "unchecked" ) public static <T> T jsonToBean(String jsonString, Class<T> beanCalss) { JSONObject jsonObject = JSONObject.fromObject(jsonString); T bean = (T) JSONObject.toBean(jsonObject, beanCalss); return bean; } /** * 将java对象转换成json字符串 * * @param bean * @return */ public static String beanToJson(Object bean) { JSONObject json = JSONObject.fromObject(bean); return json.toString(); } /** * 将java对象转换成json字符串 * * @param bean * @return */ public static String beanToJson(Object bean, String[] _nory_changes, boolean nory) { JSONObject json = null ; if (nory){ //转换_nory_changes里的属性 Field[] fields = bean.getClass().getDeclaredFields(); String str = "" ; for (Field field : fields){ // System.out.println(field.getName()); str+=( ":" +field.getName()); } fields = bean.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields){ // System.out.println(field.getName()); str+=( ":" +field.getName()); } str+= ":" ; for (String s : _nory_changes){ str = str.replace( ":" +s+ ":" , ":" ); } json = JSONObject.fromObject(bean,configJson(str.split( ":" ))); } else { //转换除了_nory_changes里的属性 json = JSONObject.fromObject(bean,configJson(_nory_changes)); } return json.toString(); } private static JsonConfig configJson(String[] excludes) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(excludes); // jsonConfig.setIgnoreDefaultExcludes( false ); // // jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); // jsonConfig.registerJsonValueProcessor(Date.class, // // new DateJsonValueProcessor(datePattern)); return jsonConfig; } /** * 将java对象List集合转换成json字符串 * @param beans * @return */ @SuppressWarnings ( "unchecked" ) public static String beanListToJson(List beans) { StringBuffer rest = new StringBuffer(); rest.append( "[" ); int size = beans.size(); for ( int i = 0 ; i < size; i++) { rest.append(beanToJson(beans.get(i))+((i<size- 1 )? "," : "" )); } rest.append( "]" ); return rest.toString(); } /** * * @param beans * @param _no_changes * @return */ @SuppressWarnings ( "unchecked" ) public static String beanListToJson(List beans, String[] _nory_changes, boolean nory) { StringBuffer rest = new StringBuffer(); rest.append( "[" ); int size = beans.size(); for ( int i = 0 ; i < size; i++) { try { rest.append(beanToJson(beans.get(i),_nory_changes,nory)); if (i<size- 1 ){ rest.append( "," ); } } catch (Exception e){ e.printStackTrace(); } } rest.append( "]" ); return rest.toString(); } /** * 从json HASH表达式中获取一个map,改map支持嵌套功能 * * @param jsonString * @return */ @SuppressWarnings ({ "unchecked" }) public static Map jsonToMap(String jsonString) { JSONObject jsonObject = JSONObject.fromObject(jsonString); Iterator keyIter = jsonObject.keys(); String key; Object value; Map valueMap = new HashMap(); while (keyIter.hasNext()) { key = (String) keyIter.next(); value = jsonObject.get(key).toString(); valueMap.put(key, value); } return valueMap; } /** * map集合转换成json格式数据 * @param map * @return */ public static String mapToJson(Map<String, ?> map, String[] _nory_changes, boolean nory){ String s_json = "{" ; Set<String> key = map.keySet(); for (Iterator<?> it = key.iterator(); it.hasNext();) { String s = (String) it.next(); if (map.get(s) == null ){ } else if (map.get(s) instanceof List<?>){ s_json+=(s+ ":" +JsonPluginsUtil.beanListToJson((List<?>)map.get(s), _nory_changes, nory)); } else { JSONObject json = JSONObject.fromObject(map); s_json += (s+ ":" +json.toString());; } if (it.hasNext()){ s_json+= "," ; } } s_json+= "}" ; return s_json; } /** * 从json数组中得到相应java数组 * * @param jsonString * @return */ public static Object[] jsonToObjectArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); } public static String listToJson(List<?> list) { JSONArray jsonArray = JSONArray.fromObject(list); return jsonArray.toString(); } /** * 从json对象集合表达式中得到一个java对象列表 * * @param jsonString * @param beanClass * @return */ @SuppressWarnings ( "unchecked" ) public static <T> List<T> jsonToBeanList(String jsonString, Class<T> beanClass) { JSONArray jsonArray = JSONArray.fromObject(jsonString); JSONObject jsonObject; T bean; int size = jsonArray.size(); List<T> list = new ArrayList<T>(size); for ( int i = 0 ; i < size; i++) { jsonObject = jsonArray.getJSONObject(i); bean = (T) JSONObject.toBean(jsonObject, beanClass); list.add(bean); } return list; } /** * 从json数组中解析出java字符串数组 * * @param jsonString * @return */ public static String[] jsonToStringArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); String[] stringArray = new String[jsonArray.size()]; int size = jsonArray.size(); for ( int i = 0 ; i < size; i++) { stringArray[i] = jsonArray.getString(i); } return stringArray; } /** * 从json数组中解析出javaLong型对象数组 * * @param jsonString * @return */ public static Long[] jsonToLongArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); int size = jsonArray.size(); Long[] longArray = new Long[size]; for ( int i = 0 ; i < size; i++) { longArray[i] = jsonArray.getLong(i); } return longArray; } /** * 从json数组中解析出java Integer型对象数组 * * @param jsonString * @return */ public static Integer[] jsonToIntegerArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); int size = jsonArray.size(); Integer[] integerArray = new Integer[size]; for ( int i = 0 ; i < size; i++) { integerArray[i] = jsonArray.getInt(i); } return integerArray; } /** * 从json数组中解析出java Double型对象数组 * * @param jsonString * @return */ public static Double[] jsonToDoubleArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); int size = jsonArray.size(); Double[] doubleArray = new Double[size]; for ( int i = 0 ; i < size; i++) { doubleArray[i] = jsonArray.getDouble(i); } return doubleArray; } } |