我在Java中使用Jackson解析器,我想以下列格式从POJO的Java对象生成JSON字符串。我怎么能这样做?
{“TOTAL”:1,“CURRENTPAGE”:1,“TOTALRECORDS”:6,“ROWS”:[{“pagename”:“Called getter Method:Test”,“pagestatus”:“Test Status” “,”“id”:4},{“pagename”:“调用getter方法:调用getter方法:测试”,“pagestatus”:“测试状态”,“id”:4},{“pagename”:“调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“,”id“:4},{”pagename“:”调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“ , “ID”:4}]}
如果我喜欢以下的东西,那么它就无法正常工作并给我错误的输出
Tblselectablecolumnjqgrid selCol = new Tblselectablecolumnjqgrid();
selCol.setPageid(4);
selCol.setPagename("Test");
selCol.setPagestatus("Test Status");
selList.add(selCol);
selList.add(selCol.clone());
selList.add(selCol.clone());
selList.add(selCol.clone());
ObjectMapper om = new ObjectMapper();
String writeValueAsString = om.writeValueAsString(selList);
System.out.println("Result: " + writeValueAsString);
JsonFactory jsf = new JsonFactory();
JsonTreeWriter tree = new JsonTreeWriter();
它为我提供了以下输出
[{“pagename”:“Called getter Method:Test”,“pagestatus”:“Test Status”,“id”:4},{“pagename”:“Called getter Method:Called getter Method:测试“,”pagestatus“:”测试状态“,”id“:4},{”pagename“:”调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“,”id“:4 },{“pagename”:“Called getter Method:Called getter Method:Test”,“pagestatus”:“Test Status”,“id”:4}]
我应该在代码中进行哪些更改才能获得所需的输出?
package com.pojo;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonCachable;
/**
* @author bhavik.ambani
*
* Useful for storing the current status of the grid in the table.
*/
public class Tblselectablecolumnjqgrid implements java.io.Serializable, Cloneable {
private static final long serialVersionUID = 1L;
@JsonProperty("id")
private int pageid;
private String pagename;
private String pagestatus;
public Tblselectablecolumnjqgrid() {
}
public Tblselectablecolumnjqgrid(int pageid, String pagename) {
this.pageid = pageid;
this.pagename = pagename;
}
public Tblselectablecolumnjqgrid(int pageid, String pagename,
String pagestatus) {
this.pageid = pageid;
this.pagename = pagename;
this.pagestatus = pagestatus;
}
public int getPageid() {
return this.pageid;
}
public void setPageid(int pageid) {
this.pageid = pageid;
}
public String getPagename() {
System.out.println("Called Getter Method");
return "Called getter Method : " + this.pagename;
}
public void setPagename(String pagename) {
this.pagename = pagename;
}
public String getPagestatus() {
return this.pagestatus;
}
public void setPagestatus(String pagestatus) {
this.pagestatus = pagestatus;
}
@Override
public Tblselectablecolumnjqgrid clone() throws CloneNotSupportedException {
Tblselectablecolumnjqgrid selBean = (Tblselectablecolumnjqgrid) super
.clone();
selBean.setPageid(getPageid());
selBean.setPagename(getPagename());
selBean.setPagestatus(getPagestatus());
return selBean;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + pageid;
result = prime * result
+ ((pagename == null) ? 0 : pagename.hashCode());
result = prime * result
+ ((pagestatus == null) ? 0 : pagestatus.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tblselectablecolumnjqgrid other = (Tblselectablecolumnjqgrid) obj;
if (pageid != other.pageid)
return false;
if (pagename == null) {
if (other.pagename != null)
return false;
} else if (!pagename.equals(other.pagename))
return false;
if (pagestatus == null) {
if (other.pagestatus != null)
return false;
} else if (!pagestatus.equals(other.pagestatus))
return false;
return true;
}
}
package com.jackson;
import java.io.IOException;
import java.util.ArrayList;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.pojo.Tblselectablecolumnjqgrid;
public class JacksonParser {
public static void main(String args[]) throws JsonGenerationException,
JsonMappingException, IOException, CloneNotSupportedException {
ArrayList<Tblselectablecolumnjqgrid> selList = new ArrayList<Tblselectablecolumnjqgrid>();
Tblselectablecolumnjqgrid selCol = new Tblselectablecolumnjqgrid();
selCol.setPageid(4);
selCol.setPagename("Test");
selCol.setPagestatus("Test Status");
selList.add(selCol);
selList.add(selCol.clone());
selList.add(selCol.clone());
selList.add(selCol.clone());
ObjectMapper om = new ObjectMapper();
String writeValueAsString = om.writeValueAsString(selList);
System.out.println("Result: " + writeValueAsString);
}
}
{“TOTAL”:1,“CURRENTPAGE”:1,“TOTALRECORDS”:6,“ROWS”:[{“pagename”:“Called getter Method:Test”,“pagestatus”:“Test Status” “,”“id”:4},{“pagename”:“调用getter方法:调用getter方法:测试”,“pagestatus”:“测试状态”,“id”:4},{“pagename”:“调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“,”id“:4},{”pagename“:”调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“ , “ID”:4}]}
[{“pagename”:“Called getter Method:Test”,“pagestatus”:“Test Status”,“id”:4},{“pagename”:“Called getter Method:Called getter Method:测试“,”pagestatus“:”测试状态“,”id“:4},{”pagename“:”调用getter方法:调用getter方法:测试“,”pagestatus“:”测试状态“,”id“:4 },{“pagename”:“Called getter Method:Called getter Method:Test”,“pagestatus”:“Test Status”,“id”:4}]
答案 0 :(得分:2)
写一个像
这样的包装器public class Wrapper {
private int TOTAL;
private int CURRENTPAGE;
private int TOTALRECORDS;
private List<Row> ROWS;
//getters setters
}
public class Row {
private String pagename;
private String pagestatus;
private int id;
//getters setters
}
在你的主要班级
//populate Wrapper
Wrapper wrap = new Wrapper();
//set attributes and Rows
...
String writeValueAsString = om.writeValueAsString(wrap);