从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 阅读(7921) 评论(0)  编辑  收藏 所属分类: ajaxjava

只有注册用户登录后才能发表评论。


网站导航: