2017-04-26 139 views
0

我有一個名爲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或我應該改變在後端查詢

+1

如果你不得不改變你發送給客戶端的東西的結構,最好做一下服務器端 – Mistalis

+1

這兩個json有什麼區別? –

+1

你只是指JSON格式或其他東西的縮進? – CrazyMac

回答

1

我希望你已經粘貼了響應上面的json。最好是更改後端代碼以獲得所需的正確json,以便您可以使用json進行進一步處理。

+0

我怎樣才能避免重複的兒童數組中的objets? –

+0

請檢查您在後端生成json的情況。 –

+0

我正在使用Jpa存儲庫方法findAll獲取數據 –