我查询mysql 得到3条list对象,伪代码如
查询的结果:
{id:1,username:'xx',age:xx,frist_name:"a"},
{id:2,username:'xx',age:xx,frist_name:"b"},
{id:3,username:'xx',age:xx,frist_name:"a"},
然后创建一个结构体
type myLists struct{
FristName string `json:"frist_name"`
Lists []UserLists `json:"lists"`
}
type UserLists struct{
Id string
Username string
Age string
}
最终想实现
{
"frist_name":"a",
"lists":[
{
"id":1,
"username":"xxx",
"age":"xxx"
},
{
"id":3,
"username":"xxx",
"age":"xxx"
},
]
},
{
"frist_name":"b",
"lists":[{
"id":2,
"username":"xxx",
"age":"xxx"
}]
}
我想把查询出来的结果放到这个结构体里面。。
请问怎么实现。
自己写一段代码实现喽,从 第一个 for 循环开始,替换为自己的数据源在处理喽.
resp,需要的话,自己再序列化下就好了