提问人:cyber_g 提问时间:7/27/2023 更新时间:7/27/2023 访问量:53
在 GO 中将 3 个 JSON 分组为一个 JSON
Grouping 3 JSONs into a JSON in GO
问:
现在,我正在为 REST API 编写 GO
现在,我得到了 3 个 JSON(str_data_table / str_all_col / str_all_domain),如下所示(这些 JSON 采用切片数据类型)
str_data_table
{
"table" : "tablea",
"table_type" : "pubilc"
}
str_all_col
[
{
"table" : "tablea",
"field" : "name",
"type". : "string"
},
{
"table" : "tablea",
"field" : "surname"
"type". : "string"
},
]
str_all_domain
[
{
"field" : "name",
"domain" : "xxx",
"element" : "jjjjj"
},
{
"field" : "name",
"domain" : "yyyy",
"element" : "ssss"
},
{
"field" : "surname",
"domain" : "aaaaaa",
"element" : "dddd"
},
{
"field" : "surname",
"domain" : "bbbbbb",
"element" : "eeee"
}
]
我需要将这 3 个 JSON 分组到一个 JSON 中,如下所示
{
"table" : "tablea",
"table_type" : "pubilc"
"field" : [
{
"field" : "name",
"type". : "string",
"detail" : [
{
"domain" : "xxx",
"element" : "jjjjj"
},
{
"domain" : "yyyy",
"element" : "ssss"
}
]
},
{
"field" : "surname",
"type". : "string",
"detail" : [
{
"domain" : "aaaaaa",
"element" : "dddd"
},
{
"domain" : "bbbbbb",
"element" : "eeee"
}
]
}
]
}
所以我使用 sqlc / chi-router / postgresql 在 GO 中编写代码
type str_domain_into struct {
Domain string
Element string
}
type str_column_info struct {
Field string
Type string
domain []str_domain_into
}
type str_table struct {
Table string
TableType string
column []str_column_info
}
var mixData []str_table
var w_mixData str_table
var w_col str_column_info
var w_domain str_domain_into
for _, dataTable := range str_data_table {
w_mixData.Table = dataTable.Table
w_mixData.TableType = dataTable.TableType
for _, dataColumn := range str_all_col {
if dataTable.Table != dataColumn.Table {
continue
}
w_col.Field = dataColumn.Field
w_col.Type = dataColumn.Type
for _, dataDomain := range str_all_domain {
if dataColumn.Field != dataDomain.Field {
continue
}
w_domain.Domain = dataDomain.Domain
w_domain.Element = dataDomain.Element
w_col.domain = append(w_col.domain, w_domain)
}
w_mixData.column = append(w_mixData.column, w_col)
}
mixData = append(mixData, w_mixData)
}
respondWithJSON(w, 200, mixData)
但是,mixData 的响应只有两个字段,如下所示
{
"table" : "tablea",
"table_type" : "pubilc"
}
func respondWithJSON 的附加代码
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
// The payload is struct data
data, err := json.Marshal(payload)
if err != nil {
log.Printf("Failed to marshal JSON response : %v", payload)
w.WriteHeader(500)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(data)
}
请帮忙。谢谢
预期结果应如下
{
"table" : "tablea",
"table_type" : "pubilc"
"field" : [
{
"field" : "name",
"type". : "string",
"detail" : [
{
"domain" : "xxx",
"element" : "jjjjj"
},
{
"domain" : "yyyy",
"element" : "ssss"
}
]
},
{
"field" : "surname",
"type". : "string",
"detail" : [
{
"domain" : "aaaaaa",
"element" : "dddd"
},
{
"domain" : "bbbbbb",
"element" : "eeee"
}
]
}
]
}
答:
0赞
cyber_g
7/27/2023
#1
谢谢你@Trock
type str_column_info struct {
Field string
Type string
domain []str_domain_into // Wrong
Domain []str_domain_into // Correct
}
type str_table struct {
Table string
TableType string
column []str_column_info // Wrong
Column []str_column_info // Correct
}
上一个:切片生成时给出的元组的元组
评论
Column
Domain