<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA—咖啡館

    ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術,交流工作經驗,分享JAVA帶來的快樂!本網站部分轉載文章,如果有版權問題請與我聯系。

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
    進入需要在項目中用java處理json格式的數據,因此封裝了一個class,現將這個class共享出來和大家分享
    通用類中用的道JAR包:http://www.tkk7.com/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 閱讀(7153) 評論(1)  編輯  收藏 所屬分類: 【JSON學習】

    Feedback

    # re: 一個java處理JSON格式數據的通用類[未登錄] 2013-01-21 14:32 f
    太全了  回復  更多評論
      

    主站蜘蛛池模板: 亚洲色无码专区在线观看| 亚洲中文字幕无码中文字在线| 久久亚洲AV成人出白浆无码国产| 你是我的城池营垒免费观看完整版 | 香蕉高清免费永久在线视频| 亚洲av乱码一区二区三区香蕉| 1000部拍拍拍18勿入免费视频下载 | 免费毛片在线看片免费丝瓜视频| 亚洲国产日韩精品| 色视频色露露永久免费观看 | 亚洲日本一区二区三区| 最近免费mv在线电影| 亚洲免费观看在线视频| 暖暖在线日本免费中文| 曰批全过程免费视频观看免费软件| 免费在线视频一区| 精品一区二区三区免费观看| 亚洲av无码国产精品色午夜字幕| 亚洲视频免费观看| 亚洲精华液一二三产区| 免费一级毛片正在播放| a毛片免费全部播放完整成| 亚洲视频一区二区在线观看| 毛色毛片免费观看| 一级毛片大全免费播放下载| 亚洲成A∨人片在线观看不卡| 亚洲视频在线观看免费视频| 亚洲精品无码久久久久久| 亚洲色婷婷综合开心网| 99免费在线观看视频| 亚洲欧洲国产综合AV无码久久| 亚洲国产成人久久一区久久| 国精产品一区一区三区免费视频| 亚洲午夜电影一区二区三区| 国产不卡免费视频| 久久久免费的精品| 小说专区亚洲春色校园| 亚洲an天堂an在线观看| 拔擦拔擦8x华人免费久久| 国产成人精品一区二区三区免费| 亚洲精品国产精品国自产网站|