標籤:json對象的封裝
public class AjaxJson {
private boolean success = true;// 是否成功
private String msg = "操作成功";// 提示資訊
private Object obj = null;// 其他資訊
private Map<String, Object> attributes;// 其他參數
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getJsonStr(){
JSONObject obj = new JSONObject();
obj.put("success", this.isSuccess());
obj.put("msg", this.getMsg());
obj.put("obj", this.obj);
obj.put("attributes", this.attributes);
return obj.toJSONString();
}
}
用法:
AjaxJson j = new AjaxJson();
.....
j.setMsg("成功!");
...
return j;
隨筆-json對象的封裝