JSON-lib转化JAVA对象为JSON格式字符串

JSON-lib转化JAVA对象为JSON格式字符串 收藏
续上文

还有一种方法是用JSON-lib转化成JSON格式字符串,需要加入json-lib-2.2-jdk13.jar和5个类库:

commons-lang-2.0.jar

commons-io-1.3.2.jar

commons-collections-3.2.jar

commons-beanutils-1.7.0.jar

ezmorph-1.0.4.jar

其中JSON-lib包可以到http://json-lib.sourceforge.net下载

如果用这种方法转化,则需要添加一个封装类:例如ListRange.java

下面是内容

view plaincopy to clipboardprint?
import java.util.ArrayList;  
import java.util.List;  
 
public class ListRange<T> {  
boolean success;  
String message;  
long totalSize;  
List<T> list;  
 
public ListRange() {  
this.totalSize = 0;  
this.list = new ArrayList<T>();  
}  
 
public long getTotalSize() {  
return totalSize;  
}  
 
public void setTotalSize(long totalSize) {  
this.totalSize = totalSize;  
}  
 
public List<T> getList() {  
return list;  
}  
 
public void setList(List<T> list) {  
this.list = list;  
}  
 
public boolean isSuccess() {  
return success;  
}  
 
public void setSuccess(boolean success) {  
this.success = success;  
}  
 
public String getMessage() {  
return message;  
}  
 
public void setMessage(String message) {  
this.message = message;  
}  
 

import java.util.ArrayList;
import java.util.List;

public class ListRange<T> {
boolean success;
String message;
long totalSize;
List<T> list;

public ListRange() {
this.totalSize = 0;
this.list = new ArrayList<T>();
}

public long getTotalSize() {
return totalSize;
}

public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}

public List<T> getList() {
return list;
}

public void setList(List<T> list) {
this.list = list;
}

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

 

 创建一个BaseAction.java

内容为:

view plaincopy to clipboardprint?
public HttpServletRequest getRequest() {  
  return ServletActionContext.getRequest();  
 }  
 
 public HttpServletResponse getResponse() {  
  return ServletActionContext.getResponse();  
 }  
 
 public void outJsonString(String str) {  
  getResponse().setContentType("text/javascript;charset=UTF-8");  
  outString(str);  
 }  
 
 public void outJson(Object obj) {  
  outJsonString(JSONObject.fromObject(obj).toString());  
 }  
 
 public void outJsonArray(Object array) {  
  outJsonArray(JSONArray.fromObject(array).toString());  
 }  
 
 public void outString(String str) {  
  try {  
   PrintWriter out = getResponse().getWriter();  
   out.write(str);  
  } catch (IOException e) {  
  }  
 }  
 
 public void outXMLString(String xmlStr) {  
  getResponse().setContentType("application/xml;charset=UTF-8");  
  outString(xmlStr);  
 } 
public HttpServletRequest getRequest() {
  return ServletActionContext.getRequest();
 }

 public HttpServletResponse getResponse() {
  return ServletActionContext.getResponse();
 }

 public void outJsonString(String str) {
  getResponse().setContentType("text/javascript;charset=UTF-8");
  outString(str);
 }

 public void outJson(Object obj) {
  outJsonString(JSONObject.fromObject(obj).toString());
 }

 public void outJsonArray(Object array) {
  outJsonArray(JSONArray.fromObject(array).toString());
 }

 public void outString(String str) {
  try {
   PrintWriter out = getResponse().getWriter();
   out.write(str);
  } catch (IOException e) {
  }
 }

 public void outXMLString(String xmlStr) {
  getResponse().setContentType("application/xml;charset=UTF-8");
  outString(xmlStr);
 }

 

创建一个TestAction.java

内容为:

view plaincopy to clipboardprint?
public void select() {  
  // TODO Auto-generated method stub  
         response.setCharacterEncoding("gb2312");  
        ListRange<TestBean> stuList = new ListRange<TestBean>();  
        List<TestBean> list = TestDAO.getList();  
        stuList.setList(list);  
        stuList.setTotalSize(list.size());  
        outJson(stuList);    
 
 } 
public void select() {
  // TODO Auto-generated method stub
         response.setCharacterEncoding("gb2312");
        ListRange<TestBean> stuList = new ListRange<TestBean>();
        List<TestBean> list = TestDAO.getList();
        stuList.setList(list);
        stuList.setTotalSize(list.size());
        outJson(stuList); 

 }

 

index.jsp页面内容为:

view plaincopy to clipboardprint?
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <title>My JSP 'index.jsp' starting page</title>    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
 <meta http-equiv="description" content="This is my page"> 
 <link rel="stylesheet" type="text/css" href="/ext2/resources/css/ext-all.css" mce_href="ext2/resources/css/ext-all.css" />   
 <mce:script type="text/javascript" src="/ext2/adapter/ext/ext-base.js" mce_src="ext2/adapter/ext/ext-base.js"></mce:script> 
 <mce:script type="text/javascript" src="/ext2/ext-all.js" mce_src="ext2/ext-all.js"></mce:script> 
    </head> 
    <body> 
        <mce:script type="text/javascript" ><!--  
Ext.onReady(function(){  
 var store=new Ext.data.JsonStore({    
  url:"test!select.ph",  
  totalProperty: "totalSize",    
      root: "list",  
  fields:[{name:'id',mapping:'id'}, {name:'name',mapping:'name'},{name:'password',mapping:'password'}]  
 });  
 store.load();  
 var gird=new Ext.grid.GridPanel({  
  renderTo:"hello",  
  title:"欢迎登录",  
  height:150,  
  width:300,  
  columns:[  
   {header:"编号",dateindex:"id"},  
   {header:"账号",dateindex:"name"},  
   {header:"密码",dateindex:"password"}  
  ],  
  store:store,  
  autoExpandColumn:2  
 })            
})  
// --></mce:script> 
       <div id="hello"> </div> 
  </body> 
</html> 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <link rel="stylesheet" type="text/css" href="/ext2/resources/css/ext-all.css" mce_href="ext2/resources/css/ext-all.css" />
 <mce:script type="text/javascript" src="/ext2/adapter/ext/ext-base.js" mce_src="ext2/adapter/ext/ext-base.js"></mce:script>
 <mce:script type="text/javascript" src="/ext2/ext-all.js" mce_src="ext2/ext-all.js"></mce:script>
    </head>
    <body>
        <mce:script type="text/javascript" ><!--
Ext.onReady(function(){
 var store=new Ext.data.JsonStore({ 
  url:"test!select.ph",
  totalProperty: "totalSize", 
      root: "list",
  fields:[{name:'id',mapping:'id'}, {name:'name',mapping:'name'},{name:'password',mapping:'password'}]
 });
 store.load();
 var gird=new Ext.grid.GridPanel({
  renderTo:"hello",
  title:"欢迎登录",
  height:150,
  width:300,
  columns:[
   {header:"编号",dateindex:"id"},
   {header:"账号",dateindex:"name"},
   {header:"密码",dateindex:"password"}
  ],
  store:store,
  autoExpandColumn:2
 })         
})
// --></mce:script>
       <div id="hello"> </div>
  </body>
</html>

 

就可以成功查询出数据了

今天开始研究这个代码,看了很久。参考了一个这样的例子,怎么弄的都没有弄出来还一直报错,后来看了一个资料书《Ext js Web应用程序开发指南》,最后在里面找到了答案,原来是我那5个类库没有导入,还有json-lib-2.2-jdk13.jar包没有导入

还有就是第一种方法,也是看了我很久不知道为什么不行,最后才注意到原来是<package name="teaweb" extends="json-default" namespace="/">这里的配置问题,终于弄出来了,感觉学到了不少东西 哈哈

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liu_moyan/archive/2009/12/01/4915786.aspx

 

 

个人签名

-------------------------------------

 

图盾 淘宝保护 保护图片 图片防盗

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值