论坛首页 Java企业应用论坛

Gson 当 bean对象里包含list 该如何转为json字符串

浏览 4352 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-05-07   最后修改:2012-05-07

我想转换成 这种 某个对象里 还包含了 数组的 这种格式,
不知道怎么做
[{"id":1,"pId":0,"name":"总部","open":true,"list":[{"id":2,"pId":1,"name":"研发部","isParent":false,"open":false},{"id":3,"pId":1,"name":"市场部","isParent":false,"open":false},{"id":4,"pId":1,"name":"开发部","isParent":false,"open":false}]}]



我的java代码这样写  转换是不成功的,那用gson我该怎么实现咧
List<FkOrganizeModel> fkOrganizeModels = fkOrganizeService.getChildrenResources(0l);
		
java.lang.reflect.Type type = new TypeToken<List<FkOrganizeModel>>(){}.getType();   
String json = gson.toJson(fkOrganizeModels,type);



FkOrganizeModel  里面  还包含了  两个list
@Entity(name = "fkOrganizeModel")
@Table(name = "fk_organize")
public class FkOrganizeModel extends BaseModel {

	private static final long serialVersionUID = 1L;

	public FkOrganizeModel() {
		super();
	}

	public FkOrganizeModel(HttpServletRequest request, String tableId) {
		super(request, tableId);
	}

	private java.lang.Long fatherId;
	private java.lang.String orgCode;
	private java.lang.String orgDesc;
	private java.lang.Integer orgGrade;
	private java.lang.String orgName;
	private java.lang.String resources;
	private List<FkUserModel> fkUsers;
	private List<FkOrganizeModel> children;

	
	
	@Column(name = "father_id", unique = false, nullable = true, insertable = true, updatable = true, length = 19)
	public java.lang.Long getFatherId() {
		return this.fatherId;
	}

	public void setFatherId(java.lang.Long fatherId) {
		this.fatherId = fatherId;
	}

	@Column(name = "org_code", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public java.lang.String getOrgCode() {
		return this.orgCode;
	}

	public void setOrgCode(java.lang.String orgCode) {
		this.orgCode = orgCode;
	}

	@Column(name = "org_desc", unique = false, nullable = true, insertable = true, updatable = true, length = 200)
	public java.lang.String getOrgDesc() {
		return this.orgDesc;
	}

	public void setOrgDesc(java.lang.String orgDesc) {
		this.orgDesc = orgDesc;
	}

	@Column(name = "org_grade", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
	public java.lang.Integer getOrgGrade() {
		return this.orgGrade;
	}

	public void setOrgGrade(java.lang.Integer orgGrade) {
		this.orgGrade = orgGrade;
	}

	@Column(name = "org_name", unique = false, nullable = false, insertable = true, updatable = true, length = 100)
	public java.lang.String getOrgName() {
		return this.orgName;
	}

	public void setOrgName(java.lang.String orgName) {
		this.orgName = orgName;
	}

	@Basic(fetch = FetchType.LAZY)
	@Column(name = "resources", unique = false, nullable = true, insertable = true, updatable = true, length = 2147483647)
	public java.lang.String getResources() {
		return this.resources;
	}

	public void setResources(java.lang.String resources) {
		this.resources = resources;
	}

	public void setFkUsers(List<FkUserModel> fkUsers) {
		this.fkUsers = fkUsers;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "orgsId")
	public List<FkUserModel> getFkUsers() {
		return fkUsers;
	}
	
	/**
	 * @return the children
	 */
	@Transient
	public List<FkOrganizeModel> getChildren() {
		return children;
	}

	/**
	 * @param children
	 *            the children to set
	 */
	public void setChildren(List<FkOrganizeModel> children) {
		this.children = children;
	}
}



   发表时间:2012-05-07  
官方文档好象有解决方案
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics