我有一个POJO,我设定了值,pojo是:
public class CreateRequisitionRO extends AbstractPortfolioSpecificRO {
private static final long serialVersionUID = 2418929142185068821L;
private BigDecimal transSrlNo;
private String transCode;
private InflowOutflow inflowOutflow;
public BigDecimal getTransSrlNo() {
return transSrlNo;
}
public void setTransSrlNo(BigDecimal transSrlNo) {
this.transSrlNo = transSrlNo;
}
public InflowOutflow getInflowOutflow() {
return inflowOutflow;
}
public void setInflowOutflow(InflowOutflow inflowOutflow) {
this.inflowOutflow = inflowOutflow;
}
public String getTransCode() {
return transCode;
}
}
这是我设置值的方式:
CreateRequisitionRO[] request = new CreateRequisitionRO[1];
request[0].setTransSrlNo(new BigDecimal(1));
request[0].setTransCode("BUY");
request[0].setInflowOutflow(InflowOutflow.I);
now i would like to convert/serialize the above java pojo to Json string.
有些人可以帮我解决这个问题吗?
最好的问候
答案 0 :(得分:7)
XStream或GSON将对您进行排序。关注JSON tutorial on XStream,您的代码将如下所示:
CreateRequisitionRO product = new CreateRequisitionRO();
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("product", Product.class);
System.out.println(xstream.toXML(product));
使用GSON,您的代码将look like this:
CreateRequisitionRO obj = new CreateRequisitionRO();
Gson gson = new Gson();
String json = gson.toJson(obj);
选择你的图书馆然后去。
答案 1 :(得分:1)
您可以使用Gson库为您提供帮助