峰之博纹 - Pelephone个人博客

java json解析类库 Jackson 基础

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

 1 public static void main(String[] args) {  
2 TestGridBean bean=null;
3 Vector vec = new Vector();
4 for(int i=0;i<1000;i++) {
5 bean = new TestGridBean();
6 bean.setNo(i);
7 bean.setName("姓名"+i);
8 bean.setMathscore(i);
9 bean.setEnscore(i);
10 vec.add(bean);
11 }
12 //ObjectMapper 线程安全具有缓存机制,重用可显著提高效率,实际使用中可设为全局公用
13 ObjectMapper mapper = new ObjectMapper();
14 //用此类构造字符串
15 StringWriter w=new StringWriter();
16 try {
17 //writeXXX方法是将对象写成JSON,readXXX方法是将JSON封装成对象
18 mapper.writeValue(w, vec);
19 } catch (JsonGenerationException e) {
20 e.printStackTrace();
21 } catch (JsonMappingException e) {
22 e.printStackTrace();
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 String s = w.toString();
27 S.echo(s);
28 }



然后是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转换的代码:

1 public void testPageData() throws Exception {  
2 String json = req.getParameter("_gt_json");
3 S.echo(json);
4 //将json转换为java bean用readXXX方法
5 Object obj = mapper.readValue(json, GTGridBean.class);
6 S.echo(obj);
7 }







posted @ 2011-10-31 16:26  Pelephone  阅读(10885)  评论(0编辑  收藏  举报