夢幻之旅

          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 閱讀(379) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 荔波县| 兴文县| 宁城县| 澄迈县| 宁明县| 汉寿县| 翼城县| 张家口市| 永胜县| 海盐县| 佛坪县| 海宁市| 万源市| 阿克| 金坛市| 德庆县| 平罗县| 乡宁县| 大兴区| 泗洪县| 彭泽县| 西峡县| 五莲县| 潜江市| 仙桃市| 四平市| 衡阳市| 金湖县| 如皋市| 兴海县| 百色市| 舞钢市| 凌源市| 宁夏| 临城县| 宾阳县| 嘉黎县| 常熟市| 望城县| 晋宁县| 科技|