struts2 action中从数据库读出来的记录,怎么封装成json格式传给客户端啊
展开全部
Ajax
json格式:
{key:value,key1:value1....}
如果返回的数据包含多个对象:
[{key1:value1...},{key2:value2....}]
json格式:
{key:value,key1:value1....}
如果返回的数据包含多个对象:
[{key1:value1...},{key2:value2....}]
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把实体的 toString 转换为
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append("{id:'" + this.id + "',");
builder.append("backColmn:'" + this.backColmn + "',");
builder.append("color:'" + this.color + "',");
builder.append("companyId:'" + this.companyId + "',");
builder.append("counts:'" + this.counts + "',");
builder.append("createBy:'" + this.createBy + "',");
builder.append("detailNo:'" + this.detailNo + "',");
builder.append("format:'" + this.format + "',");
builder.append("formatCombo:'"+this.formatCombo+"',");
builder.append("orderNo:'" + this.orderNo + "',");
builder.append("price:'" + this.price + "',");
builder.append("productName:'" + this.productName + "',");
builder.append("productNameCombo:'" + this.productNameCombo + "',"); //下拉列表值!!!
builder.append("remark:'" + this.remark + "',");
builder.append("totalPrice:'" + this.totalPrice+ "',");
builder.append("utils:'" + this.utils+ "',");
builder.append("utilsCombo:'" + this.utilsCombo+ "',"); //下拉列表值!!!
builder.append("createTime:'" + StringUtil.null2String(this.createTime) + "'}");
return builder.toString().replace("'null'", "''");}
Action
jsonData = "{success: true , data:" + class.toString()
+ "}";
this.writeJSON(jsonData);
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append("{id:'" + this.id + "',");
builder.append("backColmn:'" + this.backColmn + "',");
builder.append("color:'" + this.color + "',");
builder.append("companyId:'" + this.companyId + "',");
builder.append("counts:'" + this.counts + "',");
builder.append("createBy:'" + this.createBy + "',");
builder.append("detailNo:'" + this.detailNo + "',");
builder.append("format:'" + this.format + "',");
builder.append("formatCombo:'"+this.formatCombo+"',");
builder.append("orderNo:'" + this.orderNo + "',");
builder.append("price:'" + this.price + "',");
builder.append("productName:'" + this.productName + "',");
builder.append("productNameCombo:'" + this.productNameCombo + "',"); //下拉列表值!!!
builder.append("remark:'" + this.remark + "',");
builder.append("totalPrice:'" + this.totalPrice+ "',");
builder.append("utils:'" + this.utils+ "',");
builder.append("utilsCombo:'" + this.utilsCombo+ "',"); //下拉列表值!!!
builder.append("createTime:'" + StringUtil.null2String(this.createTime) + "'}");
return builder.toString().replace("'null'", "''");}
Action
jsonData = "{success: true , data:" + class.toString()
+ "}";
this.writeJSON(jsonData);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
String str = JSONArray.fromObject(你的值).toString(); //这里要引入net.sf.json.JSONArray这个jar包!
pw.write(str);
pw.flush();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
String str = JSONArray.fromObject(你的值).toString(); //这里要引入net.sf.json.JSONArray这个jar包!
pw.write(str);
pw.flush();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询