Nomad & His Life

          博觀而約取,厚積而薄發
          posts - 15, comments - 88, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          讓JSON.org支持JavaBean

          Posted on 2006-10-06 20:18 Nomad 閱讀(1787) 評論(1)  編輯  收藏 所屬分類: JavaAjax
          JSON.org包里的方法對JavaBean沒有支持,而JSON-lib雖然功能豐富,但是依賴很多其它的包,為了方便我就寫了一個類繼承于JSONObject,方便JavaBean的使用。

          源代碼:
          package?net.jialing;

          import?java.lang.reflect.Method;
          import?java.util.StringTokenizer;

          /**
          ?*?支持JavaBean的JSONObject
          ?*
          ?*?
          @author?Michael
          ?*?
          @since?2006-10-4
          ?*?
          @version?0.1a
          ?
          */

          public?class?JSONReflectObject?extends?JSONObject?{
          ????
          ????
          public?JSONReflectObject()?{
          ????????
          super();
          ????}
          ????
          ????
          /**
          ?????*?構造函數
          ?????*?
          @param?object?需要得到信息的JavaBean
          ?????*?
          @param?names?屬性的字符串數組
          ?????
          */
          ????
          public?JSONReflectObject(Object?object,?String?names[])?{
          ????????
          this();
          ????????
          for?(int?i?=?0;?i?<?names.length;?i?+=?1)?{
          ????????????
          try?{
          ????????????????String?name?
          =?names[i];
          ????????????????setProperty(object,name);
          ????????????}?
          catch?(Exception?e)?{
          ????????????????
          /*?forget?about?it?*/
          ????????????}
          ????????}
          ????}
          ????
          ????
          /**
          ?????*?得到JavaBean的某個屬性,支持加.得到屬性的屬性
          ?????*?
          ?????*?
          @param?owner?對象
          ?????*?
          @param?property??屬性名
          ?????*?
          @return?方法返回值
          ?????*?
          @throws?Exception
          ?????
          */
          ????
          private?void?setProperty(Object?owner,?String?property)
          ????????????
          throws?Exception?{

          ????????Class?ownerClass;
          ????????Object[]?args?
          =?null;
          ????????Class[]?argsClass?
          =?null;
          ????????

          ????????
          int?i=0,loop=0;?//i表示第幾層JSOBObject,loop表示循環了幾次
          ????????
          ????????StringTokenizer?st?
          =?new?StringTokenizer(property,".");
          ????????
          ????????JSONObject?jo[]?
          =?new?JSONObject[st.countTokens()-1];
          ????????
          for(int?x=0,y=jo.length;x<y;x++)?{
          ????????????jo[x]?
          =?new?JSONObject();
          ????????}
          ????????
          ????????
          while?(st.hasMoreTokens())?{
          ????????????String?propertyName?
          =?st.nextToken();
          ?????????????
          ??????????ownerClass?
          =?owner.getClass();
          ????????String?methodName?
          =?"get"
          ????????????????????????
          +?propertyName.substring(0,1).toUpperCase()?
          ????????????????????????
          +?propertyName.substring(1);

          ????????Method?method?
          =?ownerClass.getMethod(methodName,?argsClass);
          ????????owner?
          =?method.invoke(owner,?args);

          ????????
          if(st.hasMoreTokens())?{
          ????????????
          if(?loop?==?0)?
          ????????????????
          this.put(propertyName,jo[0]);
          ????????????
          else?
          ????????????????jo[i].put(propertyName,?jo[
          ++i]);
          ????????????????
          ????????????loop
          ++;
          ????????}
          ????????????
          else?{
          ????????????????
          if(loop==0)
          ????????????????????
          this.put(propertyName,?owner.toString());
          ????????????????
          else
          ????????????????????jo[i].put(propertyName,?owner.toString());
          ????????????}
          ????????????
          ????????}

          ????}
          ????
          }


          測試準備:

          public?class?Student?{
          ????
          private?String?name;
          ????
          private?String?email;
          ????
          private?Birthday?birthday;

          ??????getter?and?setter
          }

          public?class?Birthday?{
          ????
          private?Year?year;
          ????
          private?String?month;
          ????
          private?String?day;

          ??????
          public?Birthday(String?year,String?month,String?day){
          ????????
          this.year?=?new?Year(year);
          ????????
          this.month?=?month;
          ????????
          this.day?=?day;
          ????}

          ??????getter?and?setter
          }

          public?class?Year?{
          ????
          private?String?y;

          ??????getter?and?setter
          ????
          ????
          public?Year(String?y){
          ????????
          this.y?=?y;
          ????}
          }

          測試:
          public?class?Test?{
          ????
          ??????
          public?String?singleObject()?throws?JSONException?{
          ????????Student?s?
          =?new?Student();
          ????????s.setName(
          "Jack");
          ????????s.setEmail(
          "jack@a.com");
          ????????s.setBirthday(
          new?Birthday("1990","12","30"));
          ????????
          ????????String[]?params?
          =?{"name","email","birthday.year.y"};
          ????????
          ????????JSONReflectObject?jo?
          =?new?JSONReflectObject(s,params);
          ????????
          return?jo.toString();
          ????}

          ??????
          public?static?void?main(String?args[])?throws?Exception{
          ????????test?t?
          =?new?test();
          ????????System.out.println(t.singleObject());
          ????}
          }

          1.首先新建一個Student
          2.設置name,email,birthday屬性
          3.把要打印出的屬性放在字符串數組里,支持加"."得到屬性的屬性
          4.建立JSONReflectObject,將要輸出的對象和屬性數組作為參數
          5.打?。簕"email":"jack@a.com","name":"Jack","birthday":{"year":{"y":"1990"}}}

          評論

          # re: 讓JSON.org支持JavaBean  回復  更多評論   

          2006-10-08 12:57 by justfly
          看了看,失望,只有bean2json沒有json2bean
          而且bean2json的使用還不夠自動化。
          主站蜘蛛池模板: 广德县| 福清市| 英山县| 澳门| 佳木斯市| 长宁区| 辉县市| 磐石市| 竹溪县| 九江县| 綦江县| 江川县| 达州市| 望城县| 缙云县| 金华市| 南通市| 安徽省| 扎赉特旗| 承德县| 体育| 梧州市| 龙井市| 红桥区| 资中县| 沙河市| 武穴市| 民勤县| 高安市| 晴隆县| 静海县| 淅川县| 巩留县| 兰溪市| 东乡县| 团风县| 株洲县| 漯河市| 新邵县| 洞口县| 广州市|