JAVA—咖啡馆

——欢迎访问rogerfan的博客,常来《JAVA——咖啡馆》坐坐,喝杯浓香的咖啡,彼此探讨一下JAVA技术,交流工作经验,分享JAVA带来的快乐!本网站部分转载文章,如果有版权问题请与我联系。

BlogJava 首页 新随笔 联系 聚合 管理
  447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
进入需要在项目中用java处理json格式的数据,因此封装了一个class,现将这个class共享出来和大家分享
通用类中用的道JAR包:http://www.blogjava.net/Files/fanyingjie/json-lib-2.2.3.rar
  1 /**
  2 
  3 * Copyright (c) linkwise 2007-2009 corporation.
  4 
  5 * All rights reserved
  6 
  7 */
  8 
  9 package com.linghui.common.util;
 10 
 11 import java.util.ArrayList;
 12 
 13 import java.util.Date;
 14 
 15 import java.util.HashMap;
 16 
 17 import java.util.Iterator;
 18 
 19 import java.util.List;
 20 
 21 import java.util.Map;
 22 
 23 import net.sf.json.JSONArray;
 24 
 25 import net.sf.json.JSONObject;
 26 
 27 import net.sf.json.JsonConfig;
 28 
 29 import net.sf.json.util.CycleDetectionStrategy;
 30 
 31 import com.linghui.common.util.DateUtil;
 32 
 33 import com.linghui.common.util.jsonutil.DateJsonValueProcessor;
 34 
 35 /**
 36 
 37 *
 38 
 39 */
 40 
 41 public class JsonUtil {
 42 
 43 /**
 44 
 45 * 从一个JSON 对象字符格式中得到一个java对象
 46 
 47 @param jsonString
 48 
 49 @param pojoCalss
 50 
 51 @return
 52 
 53 */
 54 
 55 public static Object getObject4JsonString(String jsonString,Class pojoCalss){
 56 
 57 Object pojo;
 58 
 59 JSONObject jsonObject = JSONObject.fromObject( jsonString );
 60 
 61 pojo = JSONObject.toBean(jsonObject,pojoCalss);
 62 
 63 return pojo;
 64 
 65 }
 66 
 67 /**
 68 
 69 * 从json HASH表达式中获取一个map,改map支持嵌套功能
 70 
 71 @param jsonString
 72 
 73 @return
 74 
 75 */
 76 
 77 public static Map getMap4Json(String jsonString){
 78 
 79 JSONObject jsonObject = JSONObject.fromObject( jsonString );
 80 
 81 Iterator keyIter = jsonObject.keys();
 82 
 83 String key;
 84 
 85 Object value;
 86 
 87 Map valueMap = new HashMap();
 88 
 89 while( keyIter.hasNext())
 90 
 91 {
 92 
 93 key = (String)keyIter.next();
 94 
 95 value = jsonObject.get(key);
 96 
 97 valueMap.put(key, value);
 98 
 99 }
100 
101 return valueMap;
102 
103 }
104 
105 /**
106 
107 * 从json数组中得到相应java数组
108 
109 @param jsonString
110 
111 @return
112 
113 */
114 
115 public static Object getObjectArray4Json(String jsonString){
116 
117 JSONArray jsonArray = JSONArray.fromObject(jsonString);
118 
119 return jsonArray.toArray();
120 
121 }
122 
123 /**
124 
125 * 从json对象集合表达式中得到一个java对象列表
126 
127 @param jsonString
128 
129 @param pojoClass
130 
131 @return
132 
133 */
134 
135 public static List getList4Json(String jsonString, Class pojoClass){
136 
137 JSONArray jsonArray = JSONArray.fromObject(jsonString);
138 
139 JSONObject jsonObject;
140 
141 Object pojoValue;
142 
143 List list = new ArrayList();
144 
145 for ( int i = 0 ; ijsonArray.size(); i++){
146 
147 jsonObject = jsonArray.getJSONObject(i);
148 
149 pojoValue = JSONObject.toBean(jsonObject,pojoClass);
150 
151 list.add(pojoValue);
152 
153 }
154 
155 return list;
156 
157 }
158 
159 /**
160 
161 * 从json数组中解析出java字符串数组
162 
163 @param jsonString
164 
165 @return
166 
167 */
168 
169 public static String getStringArray4Json(String jsonString){
170 
171 JSONArray jsonArray = JSONArray.fromObject(jsonString);
172 
173 String stringArray = new String[jsonArray.size()];
174 
175 forint i = 0 ; ijsonArray.size() ; i++ ){
176 
177 stringArray[i] = jsonArray.getString(i);
178 
179 }
180 
181 return stringArray;
182 
183 }
184 
185 /**
186 
187 * 从json数组中解析出javaLong型对象数组
188 
189 @param jsonString
190 
191 @return
192 
193 */
194 
195 public static Long getLongArray4Json(String jsonString){
196 
197 JSONArray jsonArray = JSONArray.fromObject(jsonString);
198 
199 Long longArray = new Long[jsonArray.size()];
200 
201 forint i = 0 ; ijsonArray.size() ; i++ ){
202 
203 longArray[i] = jsonArray.getLong(i);
204 
205 }
206 
207 return longArray;
208 
209 }
210 
211 /**
212 
213 * 从json数组中解析出java Integer型对象数组
214 
215 @param jsonString
216 
217 @return
218 
219 */
220 
221 public static Integer getIntegerArray4Json(String jsonString){
222 
223 JSONArray jsonArray = JSONArray.fromObject(jsonString);
224 
225 Integer integerArray = new Integer[jsonArray.size()];
226 
227 forint i = 0 ; ijsonArray.size() ; i++ ){
228 
229 integerArray[i] = jsonArray.getInt(i);
230 
231 }
232 
233 return integerArray;
234 
235 }
236 
237 /**
238 
239 * 从json数组中解析出java Date 型对象数组,使用本方法必须保证
240 
241 @param jsonString
242 
243 @return
244 
245 */
246 
247 public static Date getDateArray4Json(String jsonString,String DataFormat){
248 
249 JSONArray jsonArray = JSONArray.fromObject(jsonString);
250 
251 Date dateArray = new Date[jsonArray.size()];
252 
253 String dateString;
254 
255 Date date;
256 
257 forint i = 0 ; ijsonArray.size() ; i++ ){
258 
259 dateString = jsonArray.getString(i);
260 
261 date = DateUtil.stringToDate(dateString, DataFormat);
262 
263 dateArray[i] = date;
264 
265 }
266 
267 return dateArray;
268 
269 }
270 
271 /**
272 
273 * 从json数组中解析出java Integer型对象数组
274 
275 @param jsonString
276 
277 @return
278 
279 */
280 
281 public static Double getDoubleArray4Json(String jsonString){
282 
283 JSONArray jsonArray = JSONArray.fromObject(jsonString);
284 
285 Double doubleArray = new Double[jsonArray.size()];
286 
287 forint i = 0 ; ijsonArray.size() ; i++ ){
288 
289 doubleArray[i] = jsonArray.getDouble(i);
290 
291 }
292 
293 return doubleArray;
294 
295 }
296 
297 /**
298 
299 * 将java对象转换成json字符串
300 
301 @param javaObj
302 
303 @return
304 
305 */
306 
307 public static String getJsonString4JavaPOJO(Object javaObj){
308 
309 JSONObject json;
310 
311 json = JSONObject.fromObject(javaObj);
312 
313 return json.toString();
314 
315 }
316 
317 /**
318 
319 * 将java对象转换成json字符串,并设定日期格式
320 
321 @param javaObj
322 
323 @param dataFormat
324 
325 @return
326 
327 */
328 
329 public static String getJsonString4JavaPOJO(Object javaObj , String dataFormat){
330 
331 JSONObject json;
332 
333 JsonConfig jsonConfig = configJson(dataFormat);
334 
335 json = JSONObject.fromObject(javaObj,jsonConfig);
336 
337 return json.toString();
338 
339 }
340 
341 /**
342 
343 @param args
344 
345 */
346 
347 public static void main(String args) {
348 
349 // TODO 自动生成方法存根
350 
351 }
352 
353 /**
354 
355 * JSON 时间解析器具
356 
357 @param datePattern
358 
359 @return
360 
361 */
362 
363 public static JsonConfig configJson(String datePattern) {
364 
365 JsonConfig jsonConfig = new JsonConfig();
366 
367 jsonConfig.setExcludes(new String{""});
368 
369 jsonConfig.setIgnoreDefaultExcludes(false);
370 
371 jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
372 
373 jsonConfig.registerJsonValueProcessor(Date.class
374 
375 new DateJsonValueProcessor(datePattern));
376 
377 return jsonConfig;
378 
379 }
380 
381 /**
382 
383 *
384 
385 @param excludes
386 
387 @param datePattern
388 
389 @return
390 
391 */
392 
393 public static JsonConfig configJson(String excludes,
394 
395 String datePattern) {
396 
397 JsonConfig jsonConfig = new JsonConfig();
398 
399 jsonConfig.setExcludes(excludes);
400 
401 jsonConfig.setIgnoreDefaultExcludes(false);
402 
403 jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
404 
405 jsonConfig.registerJsonValueProcessor(Date.class
406 
407 new DateJsonValueProcessor(datePattern));
408 
409 return jsonConfig;
410 
411 }
412 
413 }
414 
415 /**
416 
417 * linkwise
418 
419 */
420 
421 package com.linghui.common.util.jsonutil;
422 
423 import java.text.DateFormat;
424 
425 import java.text.SimpleDateFormat;
426 
427 import java.util.Date;
428 
429 import net.sf.json.JsonConfig;
430 
431 import net.sf.json.processors.JsonValueProcessor;
432 
433 /**
434 
435 @author robert.feng
436 
437 *
438 
439 */
440 
441 public class DateJsonValueProcessor implements JsonValueProcessor {
442 
443 public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
444 
445 private DateFormat dateFormat;
446 
447 /**
448 
449 * 构造方法.
450 
451 *
452 
453 @param datePattern 日期格式
454 
455 */
456 
457 public DateJsonValueProcessor(String datePattern) {
458 
459 ifnull datePattern )
460 
461 dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
462 
463 else
464 
465 dateFormat = new SimpleDateFormat(datePattern);
466 
467 }
468 
469 /* (非 Javadoc)
470 
471 * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)
472 
473 */
474 
475 public Object processArrayValue(Object arg0, JsonConfig arg1) {
476 
477 // TODO 自动生成方法存根
478 
479 return process(arg0);
480 
481 }
482 
483 /* (非 Javadoc)
484 
485 * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)
486 
487 */
488 
489 public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {
490 
491 // TODO 自动生成方法存根
492 
493 return process(arg1);
494 
495 }
496 
497 private Object process(Object value) {
498 
499 return dateFormat.format((Date) value);
500 
501 }
502 
503 }

posted on 2010-03-09 10:00 rogerfan 阅读(7141) 评论(1)  编辑  收藏 所属分类: 【JSON学习】

Feedback

# re: 一个java处理JSON格式数据的通用类[未登录] 2013-01-21 14:32 f
太全了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: