在 Oracle 中,我有这样的表:
| organization_id | ogranization_name | parent_id | level |
|-----------------|-------------------|-----------|-------|
| 1 | Facebook | | 0 |
| 2 | Instagram | 1 | 1 |
| 3 | Whatsapp | 1 | 1 |
| 4 | Dynamic | 2 | 2 |
| 5 | Google | | 0 |
此表显示组织的层次结构。我需要在我的 Go 应用程序中创建一个树状嵌套的 JSON 响应:
[
{
"organization_id": 1
"ogranization_name": "Facebook",
"childs": [
{
"organization_id": 2,
"ogranization_name": "Instagram",
"childs": null
},
{
"organization_id": 3,
"ogranization_name": "Whatsapp",
"childs": [
{
"organization_id": 4,
"ogranization_name": "Dynamic",
"childs": null
}
]
}
]
},
{
"organization_id": 5
"ogranization_name": "Google",
"childs": null
}
]
我查询 Oracle 数据库。我也创建结构。我的问题是如何知道一个组织是否有孩子?
主要去:
type Organisation struct {
ID int `json:"organization_id"`
Name string `json:"ogranization_name"`
Children []Organisation `json:"childs"`
}
rows,err := db.Query("select * from ORG_TABLE")
if err != nil {
fmt.Println(err)
return
}
var organizations []Organization
for rows.Next() {
var organization Organization
err = rows.Scan(&organization.ID, &organization.Name, ???)
if err != nil {
fmt.Println(err)
return
}
}
organizations = append(organizations, organization)
智慧大石
相关分类