Jackson是一个高效的java bean 到json的转换工具,项目主页http://jackson.codehaus.org/
Jackson提供了三种处理Json的方法,分别是:
Streaming API :基于独立事件模型处理JSON。效率最高,是下面两种方式的基础。
分别用org.codehaus.jackson.JsonParser 和 org.codehaus.jackson.JsonGenerator 读写Json
Tree Model :基于一个可变的树表示一个Json模型。最灵活
org.codehaus.jackson.map.ObjectMapper 创建树(基于JsonNode为节点)
Tree Model与XML DOM类似
Data Binding:用属性存取或者注释来处理从Json到POJO的转换。最方便,常用这种API
有简单绑定和全数据绑定,同样用org.codehaus.jackson.map.ObjectMapper处理
下面是Data Binding方法的基本使用
首先是将java bean转换为JSON
public static void main(String[] args) {
TestGridBean bean=null;
Vector vec = new Vector();
for(int i=0;i<1000;i++) {
bean = new TestGridBean();
bean.setNo(i);
bean.setName("姓名"+i);
bean.setMathscore(i);
bean.setEnscore(i);
vec.add(bean);
}
//ObjectMapper 线程安全具有缓存机制,重用可显著提高效率,实际使用中可设为全局公用
ObjectMapper mapper = new ObjectMapper();
//用此类构造字符串
StringWriter w=new StringWriter();
try {
//writeXXX方法是将对象写成JSON,readXXX方法是将JSON封装成对象
mapper.writeValue(w, vec);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String s = w.toString();
S.echo(s);
}
然后是JSon到java bean 的转换
用GT-Grid前台传入的表格参数json为例,其格式为形如:
{"recordType":"object", "pageInfo":{"pageSize":20,"pageNum":1,"totalRowNum":-1,"totalPageNum":0,"startRowNum":1,"endRowNum":-1}, "columnInfo":[ {"id":"no","header":"学号","fieldName":"no","fieldIndex":"no","sortOrder":null,"hidden":false,"exportable":true,"printable":true}, {"id":"name","header":"姓名","fieldName":"name","fieldIndex":"name","sortOrder":null,"hidden":false,"exportable":true,"printable":true}, {"id":"total","header":"总成绩","fieldName":"total","fieldIndex":"total","sortOrder":null,"hidden":false,"exportable":true,"printable":true}, {"id":"detail","header":"详细信息","fieldName":"detail","fieldIndex":"detail","sortOrder":null,"hidden":false,"exportable":true,"printable":true} ], "sortInfo":[], "filterInfo":[], "remotePaging":true, "parameters":{}, "action":"load"}
以上json对应的java bean应该是如下格式:
package gxlu.ims.webapps.action.syncResultQuery;
import java.io.Serializable;
public class GTGridBean implements Serializable {
/** * */
private static final long serialVersionUID = 1L;
private String recordType;
private String[] sortInfo;
private String[] filterInfo;
private boolean remotePaging;
private String action;
private PageInfo pageInfo;
private ColumnInfo[] columnInfo;
private Parameters parameters;
public static class PageInfo {
private String pageSize;
private long pageNum;
private long totalPageNum;
private long totalRowNum;
private long startRowNum;
private long endRowNum;
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public long getPageNum() {
return pageNum;
}
public void setPageNum(long pageNum) {
this.pageNum = pageNum;
}
public long getTotalRowNum() {
return totalRowNum;
}
public void setTotalRowNum(long totalRowNum) {
this.totalRowNum = totalRowNum;
}
public long getStartRowNum() {
return startRowNum;
}
public void setStartRowNum(long startRowNum) {
this.startRowNum = startRowNum;
}
public long getEndRowNum() {
return endRowNum;
}
public void setEndRowNum(long endRowNum) {
this.endRowNum = endRowNum;
}
public long getTotalPageNum() {
return totalPageNum;
}
public void setTotalPageNum(long totalPageNum) {
this.totalPageNum = totalPageNum;
}
}
public static class ColumnInfo {
private String id;
private String header;
private String fieldName;
private String fieldIndex;
private String sortOrder;
private boolean hidden;
private boolean exportable;
private boolean printable;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldIndex() {
return fieldIndex;
}
public void setFieldIndex(String fieldIndex) {
this.fieldIndex = fieldIndex;
}
public String getSortOrder() {
return sortOrder;
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
public boolean isHidden() {
return hidden;
}
public void setHidden(boolean hidden) {
this.hidden = hidden;
}
public boolean isExportable() {
return exportable;
}
public void setExportable(boolean exportable) {
this.exportable = exportable;
}
public boolean isPrintable() {
return printable;
}
public void setPrintable(boolean printable) {
this.printable = printable;
}
}
public static class Parameters {
}
public String getRecordType() {
return recordType;
}
public void setRecordType(String recordType) {
this.recordType = recordType;
}
public PageInfo getPageInfo() {
return pageInfo;
}
public void setPageInfo(PageInfo pageInfo) {
this.pageInfo = pageInfo;
}
public String[] getSortInfo() {
return sortInfo;
}
public void setSortInfo(String[] sortInfo) {
this.sortInfo = sortInfo;
}
public String[] getFilterInfo() {
return filterInfo;
}
public void setFilterInfo(String[] filterInfo) {
this.filterInfo = filterInfo;
}
public boolean isRemotePaging() {
return remotePaging;
}
public void setRemotePaging(boolean remotePaging) {
this.remotePaging = remotePaging;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Parameters getParameters() {
return parameters;
}
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
public ColumnInfo[] getColumnInfo() {
return columnInfo;
}
public void setColumnInfo(ColumnInfo[] columnInfo) {
this.columnInfo = columnInfo;
}
}
使用jsckson转换的代码:
public void testPageData() throws Exception {
String json = req.getParameter("_gt_json");
S.echo(json);
//将json转换为java bean用readXXX方法
Object obj = mapper.readValue(json, GTGridBean.class);
S.echo(obj);
}