Posted on 2006-10-06 20:18
Nomad 閱讀(1777)
評論(1) 編輯 收藏 所屬分類:
Java 、
Ajax
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"}}}