夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          import java.lang.reflect.*;
          import java.util.*;

          /**
           * 
          @author   惠萬鵬
           * @time     2008-06-13
           * 
          @version  1.0
           
          */

          public class JSON {
              
          /**
               * <p>
               * 本方法用利用反射機制根據對像的域名獲得PO的getter方法名
               * </p>
               * 
               * 
          @param o
               *            :po對像
               * 
          @return getter方法名
               
          */

              
          private synchronized static String[][] gFMs(Object o) {
                  String[][] fms 
          = null;
                  
          if (o != null)
                  
          {
                      Field flist[] 
          = o.getClass().getDeclaredFields();
                      fms 
          = new String[flist.length][2];
                      
          for (int i = 0; i < flist.length; i++
                      
          {
                          fms[i] 
          = JSON.assemblyFM(flist[i].toString());
                      }

                  }

                  
          return fms;
              }


              
          /**
               * <p>
               * 根據域名組裝域名和方法名
               * </p>
               * 
               * 
          @param sfield
               * 
          @return
               
          */

              
          private synchronized static String[] assemblyFM(String sfield)
              
          {
                  String[] fm 
          = new String[2];
                  
          int len = sfield.length();
                  
          int pointAt = sfield.lastIndexOf("."+ 1;
                  
                  fm[
          0= sfield.substring(pointAt, len);
                  fm[
          1= String.valueOf(fm[0].charAt(0)).toUpperCase();
                  
                  
          if (fm[0].length() > 1
                  
          {
                      fm[
          1+= fm[0].substring(1, fm[0].length());
                  }

                  
          if (sfield.indexOf("boolean"> 0)
                  
          {
                      fm[
          1= "is" + fm[1];
                  }
           
                  
          else 
                  
          {
                      fm[
          1= "get" + fm[1];
                  }

                  fm[
          1+= "()";
                  
          return fm;
              }

              
              
          /**
               * <p>po的方法里是否含有域的getter方法</p>
               * 
          @param fms
               * 
          @param method
               * 
          @return
               
          */

              
          private static String getFN(String[][] fms, String method)
              
          {
                  
          if(fms != null)
                  
          {
                      
          int len = method.length();
                      
          int pointAt = method.lastIndexOf("."+ 1;
                      String methodName 
          = method.substring(pointAt, len);
                      
          for(int i = 0; i < fms.length; i++)
                      
          {
                          
          if(methodName.equals(fms[i][1]))
                          
          {
                              
          return fms[i][0];
                          }

                      }

                  }

                  
          return "";
              }


              
          /**
               * <p>得到單個po的json info</p>
               * 
          @param o
               * 
          @return
               
          */

              
          private static String getJsonInfo(Object o) {
                  StringBuffer jsonInfo 
          = new StringBuffer("{");
                  String[][] fms 
          = JSON.gFMs(o);
                  
          if(fms != null){
                      String fn 
          = "";
                      String fv 
          = "";
                      Method[] methods 
          = o.getClass().getDeclaredMethods();
                      
          for(int i = 0; i < methods.length; i++)
                      
          {
                          fn 
          = JSON.getFN(fms,methods[i].toString());
                          
          if(fn != null && !fn.equals(""))
                          
          {
                              jsonInfo.append(
          "\"");
                              jsonInfo.append(fn);
                              jsonInfo.append(
          "\"");
                              jsonInfo.append(":");
                              jsonInfo.append(
          "\"");
                              try
                              
          {
                                  fv 
          = (String) methods[i].invoke(o, (Object[])null);
                                  fv 
          = fv == null ? "" : fv;
                                  jsonInfo.append(fv);
                              }

                              
          catch(Exception e)
                              
          {
                                  jsonInfo.append(
          "");
                              }

                              jsonInfo.append(
          "\",");
                          }

                      }

                      
                  }

                  
          int len = jsonInfo.length();
                  String strEnd 
          = jsonInfo.substring(len - 1, len);
                  
          if(strEnd != null && strEnd.equals(","))
                  
          {
                      jsonInfo.delete(len 
          - 1, len);
                  }

                  jsonInfo.append(
          "}");
                  
          return jsonInfo.toString();
              }

              
              
          /**
               * <p>得到一個集合的json信息</p>
               * 
          @param list
               * 
          @return
               
          */

              
          public static String getJsonInfos(List<Object> list){
                  StringBuffer infos 
          = new StringBuffer();
                  infos.append(
          "[");
                  
          if(list != null)
                  
          {
                      Iterator
          <Object> itObj = list.iterator();
                      
          while(itObj.hasNext())
                      
          {
                          infos.append(JSON.getJsonInfo(itObj.next()));
                          
          if(itObj.hasNext())
                          
          {
                              infos.append(
          ",");
                          }

                      }

                  }

                  infos.append(
          "]");
                  
          return infos.toString();
              }

              
          public static void main(String[] args) {
                  List
          <Object> list = new ArrayList<Object>();
                  
                  PersonPO po 
          = new PersonPO();
                  po.setAge(
          "25");
                  po.setName(
          "惠萬鵬");
                  po.setSex(
          "");
                  list.add(po);
                  
                  po 
          = new PersonPO();
                  po.setAge(
          "27");
                  po.setName(
          "惠帆");
                  po.setSex(
          "");
                  list.add(po);
                  
                  System.out.println(JSON.getJsonInfos(list));
              }


          }
          posted on 2008-06-13 14:33 HUIKK 閱讀(378) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 鄄城县| 宜兰市| 思茅市| 鹤壁市| 凯里市| 桓台县| 吉安市| 博乐市| 五大连池市| 芜湖县| 正阳县| 理塘县| 诸暨市| 兰考县| 衡阳市| 新兴县| 错那县| 西充县| 天门市| 亳州市| 寿阳县| 湘阴县| 沛县| 南城县| 灵璧县| 文成县| 广河县| 武穴市| 土默特左旗| 股票| 庆阳市| 象州县| 辽阳县| 普格县| 和龙市| 泸西县| 财经| 容城县| 安吉县| 溧阳市| 盱眙县|