我有一個名爲Activite的實體引用了自己,(Activite可以有很多activite),我試圖通過休息服務獲得所有activite。 這是實體:如何更改Json格式輸出
@Entity
public class Activite implements Serializable{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id_act;
private String designation_fr;
private String designation_ar;
private String libelle;
@ManyToOne
private Activite parent;
@OneToMany(mappedBy="parent")
private Collection<Activite> children;
public Collection<Activite> getChildren() {
return children;
}
public void setChildren(Collection<Activite> children) {
this.children = children;
}
@JsonIgnore
public Activite getParent() {
return parent;
}
public void setParent(Activite parent) {
this.parent = parent;
}
這是我得到的JSON格式:
[{
"id_act": 1,
"designation_fr": "fr",
"designation_ar": "cfd",
"libelle": "Activite 1",
"children": [
{
"id_act": 11,
"designation_fr": "bhjf",
"designation_ar": "dhbf",
"libelle": "Activite 1.1",
"children": [],
"id_prj": 1
},
{
"id_act": 12,
"designation_fr": "njkj",
"designation_ar": "ffrgrn",
"libelle": "Activite 1.2",
"children": [],
"id_prj": 1
},
{
"id_act": 13,
"designation_fr": "njkdj",
"designation_ar": "frjn",
"libelle": "Activite 1.3",
"children": [
{
"id_act": 131,
"designation_fr": "hcchchc",
"designation_ar": "GFVVHG",
"libelle": "Activite 1.3.1",
"children": [],
"id_prj": 1
}
],
"id_prj": 1
}
],
"id_prj": 1
},
{
"id_act": 11,
"designation_fr": "bhjf",
"designation_ar": "dhbf",
"libelle": "Activite 1.1",
"children": [],
"id_prj": 1
},
{
"id_act": 12,
"designation_fr": "njkj",
"designation_ar": "ffrgrn",
"libelle": "Activite 1.2",
"children": [],
"id_prj": 1
},
{
"id_act": 13,
"designation_fr": "njkdj",
"designation_ar": "frjn",
"libelle": "Activite 1.3",
"children": [
{
"id_act": 131,
"designation_fr": "hcchchc",
"designation_ar": "GFVVHG",
"libelle": "Activite 1.3.1",
"children": [],
"id_prj": 1
}
],
"id_prj": 1
},
{
"id_act": 131,
"designation_fr": "hcchchc",
"designation_ar": "GFVVHG",
"libelle": "Activite 1.3.1",
"children": [],
"id_prj": 1
}
]
,這就是我想:
[
{
"id_act": 1,
"designation_fr": "fr",
"designation_ar": "cfd",
"libelle": "Activite 1",
"children": [
{
"id_act": 11,
"designation_fr": "bhjf",
"designation_ar": "dhbf",
"libelle": "Activite 1.1",
"children": [],
"id_prj": 1
},
{
"id_act": 12,
"designation_fr": "njkj",
"designation_ar": "ffrgrn",
"libelle": "Activite 1.2",
"children": [],
"id_prj": 1
},
{
"id_act": 13,
"designation_fr": "njkdj",
"designation_ar": "frjn",
"libelle": "Activite 1.3",
"children": [{
"id_act": 131,
"designation_fr": "njkdj",
"designation_ar": "frjn",
"libelle": "Activite 1.3.1",
"children": [],
"id_prj": 1
}],
"id_prj": 1
}
],
"id_prj": 1
}]
正如你所看到的激活與編號11 12 13和131是重複的我希望他們只在兒童數組 我實際上在調用angularjs中的其餘服務,所以是否有可能改變j兒子格式,我與angularjs或我應該改變在後端查詢
如果你不得不改變你發送給客戶端的東西的結構,最好做一下服務器端 – Mistalis
這兩個json有什麼區別? –
你只是指JSON格式或其他東西的縮進? – CrazyMac