java中操作JSON公共类

  •    package com.linghui.common.util;   

  •   import java.util.ArrayList;   
  •   
  •   import java.util.Date;   
  •   
  •   import java.util.HashMap;   
  •   
  •   import java.util.Iterator;   
  •   
  •   import java.util.List;   
  •   
  •   import java.util.Map;   
  •   
  •   import net.sf.json.JSONArray;   
  •   
  •   import net.sf.json.JSONObject;   
  •   
  •   import net.sf.json.JsonConfig;   
  •   
  •   import net.sf.json.util.CycleDetectionStrategy;   
  •   
  •   import com.linghui.common.util.DateUtil;   
  •   
  •   import com.linghui.common.util.jsonutil.DateJsonValueProcessor;   
  •   
  •   /**   
  •   
  •   *   
  •   
  •   */   
  •   
  •   public class JsonUtil {   
  •   
  •   /**   
  •   
  •   * 从一个JSON 对象字符格式中得到一个java对象   
  •   
  •   * @param jsonString   
  •   
  •   * @param pojoCalss   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Object getObject4JsonString(String jsonString,Class pojoCalss){   
  •   
  •   Object pojo;   
  •   
  •   JSONObject jsonObject = JSONObject.fromObject( jsonString );   
  •   
  •   pojo = JSONObject.toBean(jsonObject,pojoCalss);   
  •   
  •   return pojo;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json HASH表达式中获取一个map,改map支持嵌套功能   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Map getMap4Json(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);   
  •   
  •   valueMap.put(key, value);   
  •   
  •   }   
  •   
  •   return valueMap;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中得到相应java数组   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Object[] getObjectArray4Json(String jsonString){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   return jsonArray.toArray();   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json对象集合表达式中得到一个java对象列表   
  •   
  •   * @param jsonString   
  •   
  •   * @param pojoClass   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static List getList4Json(String jsonString, Class pojoClass){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   JSONObject jsonObject;   
  •   
  •   Object pojoValue;   
  •   
  •   List list = new ArrayList();   
  •   
  •   for ( int i = 0 ; i<jsonArray.size(); i++){   
  •   
  •   jsonObject = jsonArray.getJSONObject(i);   
  •   
  •   pojoValue = JSONObject.toBean(jsonObject,pojoClass);   
  •   
  •   list.add(pojoValue);   
  •   
  •   }   
  •   
  •   return list;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中解析出java字符串数组   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static String[] getStringArray4Json(String jsonString){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   String[] stringArray = new String[jsonArray.size()];   
  •   
  •   for( int i = 0 ; i<jsonArray.size() ; i++ ){   
  •   
  •   stringArray = jsonArray.getString(i);   
  •   
  •   }   
  •   
  •   return stringArray;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中解析出javaLong型对象数组   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Long[] getLongArray4Json(String jsonString){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   Long[] longArray = new Long[jsonArray.size()];   
  •   
  •   for( int i = 0 ; i<jsonArray.size() ; i++ ){   
  •   
  •   longArray = jsonArray.getLong(i);   
  •   
  •   }   
  •   
  •   return longArray;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中解析出java Integer型对象数组   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Integer[] getIntegerArray4Json(String jsonString){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   Integer[] integerArray = new Integer[jsonArray.size()];   
  •   
  •   for( int i = 0 ; i<jsonArray.size() ; i++ ){   
  •   
  •   integerArray = jsonArray.getInt(i);   
  •   
  •   }   
  •   
  •   return integerArray;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中解析出java Date 型对象数组,使用本方法必须保证   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Date[] getDateArray4Json(String jsonString,String DataFormat){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   Date[] dateArray = new Date[jsonArray.size()];   
  •   
  •   String dateString;   
  •   
  •   Date date;   
  •   
  •   for( int i = 0 ; i<jsonArray.size() ; i++ ){   
  •   
  •   dateString = jsonArray.getString(i);   
  •   
  •   date = DateUtil.stringToDate(dateString, DataFormat);   
  •   
  •   dateArray = date;   
  •   
  •   }   
  •   
  •   return dateArray;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 从json数组中解析出java Integer型对象数组   
  •   
  •   * @param jsonString   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static Double[] getDoubleArray4Json(String jsonString){   
  •   
  •   JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  •   
  •   Double[] doubleArray = new Double[jsonArray.size()];   
  •   
  •   for( int i = 0 ; i<jsonArray.size() ; i++ ){   
  •   
  •   doubleArray = jsonArray.getDouble(i);   
  •   
  •   }   
  •   
  •   return doubleArray;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 将java对象转换成json字符串   
  •   
  •   * @param javaObj   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static String getJsonString4JavaPOJO(Object javaObj){   
  •   
  •   JSONObject json;   
  •   
  •   json = JSONObject.fromObject(javaObj);   
  •   
  •   return json.toString();   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * 将java对象转换成json字符串,并设定日期格式   
  •   
  •   * @param javaObj   
  •   
  •   * @param dataFormat   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static String getJsonString4JavaPOJO(Object javaObj , String dataFormat){   
  •   
  •   JSONObject json;   
  •   
  •   JsonConfig jsonConfig = configJson(dataFormat);   
  •   
  •   json = JSONObject.fromObject(javaObj,jsonConfig);   
  •   
  •   return json.toString();   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * @param args   
  •   
  •   */   
  •   
  •   public static void main(String[] args) {   
  •   
  •   // TODO 自动生成方法存根   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   * JSON 时间解析器具   
  •   
  •   * @param datePattern   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static JsonConfig configJson(String datePattern) {   
  •   
  •   JsonConfig jsonConfig = new JsonConfig();   
  •   
  •   jsonConfig.setExcludes(new String[]{""});   
  •   
  •   jsonConfig.setIgnoreDefaultExcludes(false);   
  •   
  •   jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
  •   
  •   jsonConfig.registerJsonValueProcessor(Date.class,   
  •   
  •   new DateJsonValueProcessor(datePattern));   
  •   
  •   return jsonConfig;   
  •   
  •   }   
  •   
  •   /**   
  •   
  •   *   
  •   
  •   * @param excludes   
  •   
  •   * @param datePattern   
  •   
  •   * @return   
  •   
  •   */   
  •   
  •   public static JsonConfig configJson(String[] excludes,   
  •   
  •   String datePattern) {   
  •   
  •   JsonConfig jsonConfig = new JsonConfig();   
  •   
  •   jsonConfig.setExcludes(excludes);   
  •   
  •   jsonConfig.setIgnoreDefaultExcludes(false);   
  •   
  •   jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
  •   
  •   jsonConfig.registerJsonValueProcessor(Date.class,   
  •   
  •   new DateJsonValueProcessor(datePattern));   
  •   
  •   return jsonConfig;   
  •   
  •   }   
  •   
  •   } /**   
  •   
  •   * linkwise   
  •   
  •   */   
  •   
  •   package com.linghui.common.util.jsonutil;   
  •   
  •   import java.text.DateFormat;   
  •   
  •   import java.text.SimpleDateFormat;   
  •   
  •   import java.util.Date;   
  •   
  •   import net.sf.json.JsonConfig;   
  •   
  •   import net.sf.json.processors.JsonValueProcessor;   
  •   
  •   /**   
  •   
  •   * @author robert.feng   
  •   
  •   *   
  •   
  •   */   
  •   
  •   public class DateJsonValueProcessor implements JsonValueProcessor {   
  •   
  •   public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";   
  •   
  •   private DateFormat dateFormat;   
  •   
  •   /**   
  •   
  •   * 构造方法.   
  •   
  •   *   
  •   
  •   * @param datePattern 日期格式   
  •   
  •   */   
  •   
  •   public DateJsonValueProcessor(String datePattern) {   
  •   
  •   if( null == datePattern )   
  •   
  •   dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);   
  •   
  •   else   
  •   
  •   dateFormat = new SimpleDateFormat(datePattern);   
  •   
  •   }   
  •   
  •   /* (非 Javadoc)   
  •   
  •   * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)   
  •   
  •   */   
  •   
  •   public Object processArrayValue(Object arg0, JsonConfig arg1) {   
  •   
  •   // TODO 自动生成方法存根   
  •   
  •   return process(arg0);   
  •   
  •   }   
  •   
  •   /* (非 Javadoc)   
  •   
  •   * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)   
  •   
  •   */   
  •   
  •   public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) {   
  •   
  •   // TODO 自动生成方法存根   
  •   
  •   return process(arg1);   
  •   
  •   }   
  •   
  •   private Object process(Object value) {   
  •   
  •   return dateFormat.format((Date) value);   
  •   
  •   }   
  •   
  •   }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值