從Java 類產生json(json-lib)

          ???json-lib的依賴包 :
          ????????????commons-logging.jar?
          ????????????common.lang.jar
          ??????????? xom.jar(XMLObject Model)
          ??????????? commons-beanutils.jar
          ????????????commons-collections-3.0.jar
          ????????????ezmorph-0.9.2.jar

          ? 大概就是這些,第一個例子搞了一一個小時,依賴包太多了
          ?
          ?? json-lib可以將java中的Array,Collections,Object,XML轉化成json對象
          ????
          ???? ?http://json-lib.sourceforge.net/usage.html

          我按照官方文檔寫的例子:
          ???????
          import?java.util.ArrayList;
          import?java.util.HashMap;
          import?java.util.List;
          import?java.util.Map;


          import?net.sf.json.*;
          public?class?Test?{

          ????
          /**
          ?????*?
          @param?args
          ?????
          */
          ????
          private?static?boolean[]?boolArray={true,false};
          ????
          private?static?List?list?=?new?ArrayList();???
          ????
          private?static?Map?map=new?HashMap();
          ????
          private?static?Object[]?test={new?TestBean(),new?TestBean()};
          ????
          public?static?void?main(String[]?args)?{
          ????
          ????
          //轉換Array
          ????JSONArray?jsonArray?=?JSONArray.fromObject(?boolArray?);???
          ????
          //System.out.println(jsonArray.toString());
          ????JSONArray?jsonArray1=JSONArray.fromObject("['1','2','3']");
          ????
          //System.out.println(jsonArray1.toString());
          ????
          ????list.add(
          "first");???
          ????list.add(
          "second");???
          ????
          //list.add(new?TestBean());
          ????
          //轉換List
          ????JSONArray?jsonList?=?JSONArray.fromObject(list);???
          ????System.out.println(jsonList);???

          ????
          //轉換map
          ????map.put("name",?"windfree");
          ????map.put(
          "bool",?Boolean.TRUE);
          ????map.put(
          "int",new?Integer(0));
          ????map.put(?
          "arr",?new?String[]{"a","b"}?);
          ????map.put(
          "func",?"function?(i){?return?this.arr[i];?}");
          ????map.put(
          "bean",test);
          ????JSONObject?jsonMap?
          =?JSONObject.fromObject(map);???
          ????
          //System.out.println(jsonMap);???

          ????
          //轉換Bean
          ????JSONObject?jsonBean=JSONObject.fromObject(new?TestBean());
          ????
          //System.out.println(jsonBean);
          ????


          ????

          ????}

          }


          輸出結果
          [true,false]
          ["1","2","3"]
          ["first","second"]
          {"bean":[{"name":"windfree","pojoId":1,"func1":function(i){?return?this.options[i];?},"func2":function(i){?return?this.options[i];?},"options":["a","f"]},
          {"name":"windfree","pojoId":1,
          "func1":function(i){?return?this.options[i];?},
          "func2":function(i){?return?this.options[i];?},
          "options":["a","f"]}],"arr":["a","b"],"int":0,"name":"windfree",
          "func":function?(i){?return?this.arr[i];?},
          "bool":true}
          {"name":"windfree","pojoId":1,"func1":function(i){?return?this.options[i];?},"func2":function(i){?return?this.options[i];?},"options":["a","f"]}

          最近也在看GWT,GWT包中有一關于json的例子,
          ? http://code.google.com/webtoolkit/documentation/examples/jsonrpc/demo.html
          ?例子中的json對象為一個js文件,我用servlet重新寫了一下。

          Thumbnail類:
          ???????????????????
          package?com.windfree.server;

          public?class?Thumbnail?{
          ????
          private?String?Url="http://scd.mm-c1.yimg.com/image/1135881977";
          ????
          private?int?Height=120;
          ????
          private?int?Width=99;
          ????
          public?int?getHeight()?{
          ????????
          return?Height;
          ????}
          ????
          public?void?setHeight(int?height)?{
          ????????Height?
          =?height;
          ????}
          ????
          public?String?getUrl()?{
          ????????
          return?Url;
          ????}
          ????
          public?void?setUrl(String?url)?{
          ????????Url?
          =?url;
          ????}
          ????
          public?int?getWidth()?{
          ????????
          return?Width;
          ????}
          ????
          public?void?setWidth(int?width)?{
          ????????Width?
          =?width;
          ????}
          ????
          public?Thumbnail(String?url,?int?height,?int?width)?{
          ????????
          super();
          ????????Url?
          =?url;
          ????????Height?
          =?height;
          ????????Width?
          =?width;
          ????}
          }


          Result類:
          ??
          package?com.windfree.server;

          public?class?Result?{
          ????
          private?String?Title;
          ????
          private?String?Url;
          ????
          private?String?ClickUrl;
          ????
          private?String?RefererUrl;
          ????
          private?String?FileFormat;
          ????
          private?int?Height;
          ????
          private?int?Width;
          ????
          private?Thumbnail?thumbnail;
          ????
          public?Result(String?title,?String?url,?String?clickUrl,?String?refererUrl,?String?fileFormat,?int?height,?int?width,?Thumbnail?thumbnail)?{
          ????????
          super();
          ????????Title?
          =?title;
          ????????Url?
          =?url;
          ????????ClickUrl?
          =?clickUrl;
          ????????RefererUrl?
          =?refererUrl;
          ????????FileFormat?
          =?fileFormat;
          ????????Height?
          =?height;
          ????????Width?
          =?width;
          ????????
          this.thumbnail?=?thumbnail;
          ????}
          ????
          public?String?getClickUrl()?{
          ????????
          return?ClickUrl;
          ????}
          ????
          public?void?setClickUrl(String?clickUrl)?{
          ????????ClickUrl?
          =?clickUrl;
          ????}
          ????
          public?String?getFileFormat()?{
          ????????
          return?FileFormat;
          ????}
          ????
          public?void?setFileFormat(String?fileFormat)?{
          ????????FileFormat?
          =?fileFormat;
          ????}
          ????
          public?int?getHeight()?{
          ????????
          return?Height;
          ????}
          ????
          public?void?setHeight(int?height)?{
          ????????Height?
          =?height;
          ????}
          ????
          public?String?getRefererUrl()?{
          ????????
          return?RefererUrl;
          ????}
          ????
          public?void?setRefererUrl(String?refererUrl)?{
          ????????RefererUrl?
          =?refererUrl;
          ????}
          ????
          public?Thumbnail?getThumbnail()?{
          ????????
          return?thumbnail;
          ????}
          ????
          public?void?setThumbnail(Thumbnail?thumbnail)?{
          ????????
          this.thumbnail?=?thumbnail;
          ????}
          ????
          public?String?getTitle()?{
          ????????
          return?Title;
          ????}
          ????
          public?void?setTitle(String?title)?{
          ????????Title?
          =?title;
          ????}
          ????
          public?String?getUrl()?{
          ????????
          return?Url;
          ????}
          ????
          public?void?setUrl(String?url)?{
          ????????Url?
          =?url;
          ????}
          ????
          public?int?getWidth()?{
          ????????
          return?Width;
          ????}
          ????
          public?void?setWidth(int?width)?{
          ????????Width?
          =?width;
          ????}
          }


          JSONHttpServlet類:
          ???
          package?com.windfree.server;

          import?java.io.IOException;
          import?java.io.PrintWriter;
          import?java.util.HashMap;
          import?java.util.Map;

          import?javax.servlet.ServletException;
          import?javax.servlet.http.HttpServlet;
          import?javax.servlet.http.HttpServletRequest;
          import?javax.servlet.http.HttpServletResponse;

          import?net.sf.json.JSONObject;

          public?class?JSONHttpServlet?extends?HttpServlet?{

          ????
          /**
          ?????*?
          ?????
          */
          ????
          private?static?final?long?serialVersionUID?=?1L;
          ????
          private?static?Map?map=new?HashMap();
          ????
          private?static?Map?map2=new?HashMap();
          ????@Override
          ????
          protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{
          ????????
          this.doPost(request,?response);?
          ????}

          ????@Override
          ????
          protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{
          ????????Thumbnail?tmail1
          =new?Thumbnail("http://scd.mm-c1.yimg.com/image/1135881977",200,203);
          ????????Thumbnail?tmail2
          =new?Thumbnail("http://scd.mm-c1.yimg.com/image/1135881977",100,100);
          ????????Result?reulst1
          =new?Result("111","222","333","444","555",100,100,tmail1);
          ????????Result?reulst2
          =new?Result("111","222","333","444","555",100,100,tmail2);
          ????????map.put(
          "totalResultsAvailable",?"144429");
          ????????map.put(
          "totalResultsReturned",?2);
          ????????map.put(
          "firstResultPosition",?1);
          ????????map.put(
          "result",new?Result[]{reulst1,reulst2});
          ????????map2.put(
          "Results",?map);
          ????????JSONObject?jsonMap
          =JSONObject.fromObject(map2);
          ????????PrintWriter?out?
          =?response.getWriter();
          ????????out.println(jsonMap.toString());
          ????}

          }

          在例子中的配置文件中的配置文件(JSON.gwt.xml)增加
          ???<servlet path="/jsonTest"
          ? ?? class="com.windfree.server.JSONHttpServlet"/>
          同時將JSON.java中的URL改為/jsonTest。就可以了。
          ???結果:
          ?????????
          ?

          posted on 2007-01-12 16:22 windfree 閱讀(7931) 評論(0)  編輯  收藏 所屬分類: ajaxjava

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 民勤县| 卓尼县| 金门县| 左权县| 来凤县| 自贡市| 射阳县| 磐安县| 宝兴县| 普定县| 石棉县| 清新县| 日土县| 象州县| 望谟县| 开封市| 南昌县| 青河县| 梅州市| 慈溪市| 淳化县| 芜湖县| 师宗县| 阿拉善右旗| 文登市| 和静县| 宁波市| 昭通市| 轮台县| 长丰县| 重庆市| 区。| 柘荣县| 黎平县| 西乡县| 桑日县| 册亨县| 五指山市| 丹寨县| 新丰县| 吉首市|