java中json处理的使用

来源:互联网 发布:三星手机官方软件 编辑:程序博客网 时间:2024/03/29 18:14
Java代码
  1. /**  
  2.  * Copyright (c) linkwise 2007-2009 corporation.    
  3.  * All rights reserved  
  4.  */  
  5. package com.linghui.common.util;   
  6.   
  7. import java.util.ArrayList;   
  8. import java.util.Date;   
  9. import java.util.HashMap;   
  10. import java.util.Iterator;   
  11. import java.util.List;   
  12. import java.util.Map;   
  13.   
  14. import net.sf.json.JSONArray;   
  15. import net.sf.json.JSONObject;   
  16. import net.sf.json.JsonConfig;   
  17. import net.sf.json.util.CycleDetectionStrategy;   
  18.   
  19. import com.linghui.common.util.DateUtil;   
  20. import com.linghui.common.util.jsonutil.DateJsonValueProcessor;   
  21.   
  22. /** *//**  
  23.  * @author </br> <a href="mailto:fx19800215@163.com"> robert.feng</a>  
  24.  *  
  25.  */  
  26. public class JsonUtil ...{   
  27.   
  28.     /** *//**  
  29.      * 从一个JSON 对象字符格式中得到一个java对象  
  30.      * @param jsonString  
  31.      * @param pojoCalss  
  32.      * @return  
  33.      */  
  34.     public static Object getObject4JsonString(String jsonString,Class pojoCalss)...{   
  35.         Object pojo;   
  36.         JSONObject jsonObject = JSONObject.fromObject( jsonString );     
  37.         pojo = JSONObject.toBean(jsonObject,pojoCalss);   
  38.         return pojo;   
  39.     }   
  40.        
  41.        
  42.        
  43.     /** *//**  
  44.      * 从json HASH表达式中获取一个map,改map支持嵌套功能  
  45.      * @param jsonString  
  46.      * @return  
  47.      */  
  48.     public static Map getMap4Json(String jsonString)...{   
  49.         JSONObject jsonObject = JSONObject.fromObject( jsonString );     
  50.         Iterator  keyIter = jsonObject.keys();   
  51.         String key;   
  52.         Object value;   
  53.         Map valueMap = new HashMap();   
  54.   
  55.         while( keyIter.hasNext())   
  56.         ...{   
  57.             key = (String)keyIter.next();   
  58.             value = jsonObject.get(key);   
  59.             valueMap.put(key, value);   
  60.         }   
  61.            
  62.         return valueMap;   
  63.     }   
  64.        
  65.        
  66.     /** *//**  
  67.      * 从json数组中得到相应java数组  
  68.      * @param jsonString  
  69.      * @return  
  70.      */  
  71.     public static Object[] getObjectArray4Json(String jsonString)...{   
  72.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  73.         return jsonArray.toArray();   
  74.            
  75.     }   
  76.        
  77.        
  78.     /** *//**  
  79.      * 从json对象集合表达式中得到一个java对象列表  
  80.      * @param jsonString  
  81.      * @param pojoClass  
  82.      * @return  
  83.      */  
  84.     public static List getList4Json(String jsonString, Class pojoClass)...{   
  85.            
  86.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  87.         JSONObject jsonObject;   
  88.         Object pojoValue;   
  89.            
  90.         List list = new ArrayList();   
  91.         for ( int i = 0 ; i<jsonArray.size(); i++)...{   
  92.                
  93.             jsonObject = jsonArray.getJSONObject(i);   
  94.             pojoValue = JSONObject.toBean(jsonObject,pojoClass);   
  95.             list.add(pojoValue);   
  96.                
  97.         }   
  98.         return list;   
  99.   
  100.     }   
  101.        
  102.     /** *//**  
  103.      * 从json数组中解析出java字符串数组  
  104.      * @param jsonString  
  105.      * @return  
  106.      */  
  107.     public static String[] getStringArray4Json(String jsonString)...{   
  108.            
  109.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  110.         String[] stringArray = new String[jsonArray.size()];   
  111.         forint i = 0 ; i<jsonArray.size() ; i++ )...{   
  112.             stringArray[i] = jsonArray.getString(i);   
  113.                
  114.         }   
  115.            
  116.         return stringArray;   
  117.     }   
  118.        
  119.     /** *//**  
  120.      * 从json数组中解析出javaLong型对象数组  
  121.      * @param jsonString  
  122.      * @return  
  123.      */  
  124.     public static Long[] getLongArray4Json(String jsonString)...{   
  125.            
  126.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  127.         Long[] longArray = new Long[jsonArray.size()];   
  128.         forint i = 0 ; i<jsonArray.size() ; i++ )...{   
  129.             longArray[i] = jsonArray.getLong(i);   
  130.                
  131.         }   
  132.         return longArray;   
  133.     }   
  134.        
  135.     /** *//**  
  136.      * 从json数组中解析出java Integer型对象数组  
  137.      * @param jsonString  
  138.      * @return  
  139.      */  
  140.     public static Integer[] getIntegerArray4Json(String jsonString)...{   
  141.            
  142.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  143.         Integer[] integerArray = new Integer[jsonArray.size()];   
  144.         forint i = 0 ; i<jsonArray.size() ; i++ )...{   
  145.             integerArray[i] = jsonArray.getInt(i);   
  146.                
  147.         }   
  148.         return integerArray;   
  149.     }   
  150.        
  151.     /** *//**  
  152.      * 从json数组中解析出java Date 型对象数组,使用本方法必须保证  
  153.      * @param jsonString  
  154.      * @return  
  155.      */  
  156.     public static Date[] getDateArray4Json(String jsonString,String DataFormat)...{   
  157.            
  158.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  159.         Date[] dateArray = new Date[jsonArray.size()];   
  160.         String dateString;   
  161.         Date date;   
  162.            
  163.         forint i = 0 ; i<jsonArray.size() ; i++ )...{   
  164.             dateString = jsonArray.getString(i);   
  165.             date = DateUtil.stringToDate(dateString, DataFormat);   
  166.             dateArray[i] = date;   
  167.                
  168.         }   
  169.         return dateArray;   
  170.     }   
  171.        
  172.     /** *//**  
  173.      * 从json数组中解析出java Integer型对象数组  
  174.      * @param jsonString  
  175.      * @return  
  176.      */  
  177.     public static Double[] getDoubleArray4Json(String jsonString)...{   
  178.            
  179.         JSONArray jsonArray = JSONArray.fromObject(jsonString);   
  180.         Double[] doubleArray = new Double[jsonArray.size()];   
  181.         forint i = 0 ; i<jsonArray.size() ; i++ )...{   
  182.             doubleArray[i] = jsonArray.getDouble(i);   
  183.                
  184.         }   
  185.         return doubleArray;   
  186.     }   
  187.        
  188.        
  189.     /** *//**  
  190.      * 将java对象转换成json字符串  
  191.      * @param javaObj  
  192.      * @return  
  193.      */  
  194.     public static String getJsonString4JavaPOJO(Object javaObj)...{   
  195.            
  196.         JSONObject json;   
  197.         json = JSONObject.fromObject(javaObj);   
  198.         return json.toString();   
  199.            
  200.     }   
  201.        
  202.        
  203.        
  204.        
  205.     /** *//**  
  206.      * 将java对象转换成json字符串,并设定日期格式  
  207.      * @param javaObj  
  208.      * @param dataFormat  
  209.      * @return  
  210.      */  
  211.     public static String getJsonString4JavaPOJO(Object javaObj , String dataFormat)...{   
  212.            
  213.         JSONObject json;   
  214.         JsonConfig jsonConfig = configJson(dataFormat);   
  215.         json = JSONObject.fromObject(javaObj,jsonConfig);   
  216.         return json.toString();   
  217.            
  218.            
  219.     }   
  220.        
  221.        
  222.        
  223.     /** *//**  
  224.      * @param args  
  225.      */  
  226.     public static void main(String[] args) ...{   
  227.         // TODO 自动生成方法存根   
  228.   
  229.     }   
  230.        
  231.     /** *//**  
  232.      * JSON 时间解析器具  
  233.      * @param datePattern  
  234.      * @return  
  235.      */  
  236.     public static JsonConfig configJson(String datePattern) ...{      
  237.             JsonConfig jsonConfig = new JsonConfig();      
  238.             jsonConfig.setExcludes(new String[]...{""});      
  239.             jsonConfig.setIgnoreDefaultExcludes(false);      
  240.             jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);      
  241.             jsonConfig.registerJsonValueProcessor(Date.class,      
  242.                 new DateJsonValueProcessor(datePattern));      
  243.              
  244.             return jsonConfig;      
  245.         }     
  246.        
  247.     /** *//**  
  248.      *   
  249.      * @param excludes  
  250.      * @param datePattern  
  251.      * @return  
  252.      */  
  253.     public static JsonConfig configJson(String[] excludes,      
  254.             String datePattern) ...{      
  255.             JsonConfig jsonConfig = new JsonConfig();      
  256.             jsonConfig.setExcludes(excludes);      
  257.             jsonConfig.setIgnoreDefaultExcludes(false);      
  258.             jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);      
  259.             jsonConfig.registerJsonValueProcessor(Date.class,      
  260.                 new DateJsonValueProcessor(datePattern));      
  261.              
  262.             return jsonConfig;      
  263.         }     
  264.   
  265. }  

 

Java代码 复制代码
  1. /** *//**  
  2.  * linkwise  
  3.  */  
  4. package com.linghui.common.util.jsonutil;   
  5.   
  6. import java.text.DateFormat;   
  7. import java.text.SimpleDateFormat;   
  8. import java.util.Date;   
  9.   
  10. import net.sf.json.JsonConfig;   
  11. import net.sf.json.processors.JsonValueProcessor;   
  12.   
  13.   
  14.   
  15. /** *//**  
  16.  *  @author </br> <a href="mailto:fx19800215@163.com"> robert.feng</a>  
  17.  *  
  18.  */  
  19. public class DateJsonValueProcessor implements JsonValueProcessor ...{   
  20.   
  21.        
  22.     public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";      
  23.     private DateFormat dateFormat;      
  24.   
  25.        
  26.        
  27.     /** *//**    
  28.      * 构造方法.    
  29.      *    
  30.      * @param datePattern 日期格式    
  31.      */     
  32.     public DateJsonValueProcessor(String datePattern) ...{      
  33.              
  34.         ifnull == datePattern )   
  35.             dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);     
  36.         else  
  37.             dateFormat = new SimpleDateFormat(datePattern);    
  38.            
  39.     }      
  40.   
  41.        
  42.        
  43.     /**//* (非 Javadoc)  
  44.      * @see net.sf.json.processors.JsonValueProcessor#processArrayValue(java.lang.Object, net.sf.json.JsonConfig)  
  45.      */  
  46.     public Object processArrayValue(Object arg0, JsonConfig arg1) ...{   
  47.         // TODO 自动生成方法存根   
  48.         return process(arg0);      
  49.     }   
  50.   
  51.     /**//* (非 Javadoc)  
  52.      * @see net.sf.json.processors.JsonValueProcessor#processObjectValue(java.lang.String, java.lang.Object, net.sf.json.JsonConfig)  
  53.      */  
  54.     public Object processObjectValue(String arg0, Object arg1, JsonConfig arg2) ...{   
  55.         // TODO 自动生成方法存根   
  56.         return process(arg1);      
  57.     }   
  58.        
  59.     private Object process(Object value) ...{      
  60.         return dateFormat.format((Date) value);      
  61.     }      
  62.   
  63.   
  64. }