1 回答
TA贡献2016条经验 获得超9个赞
您需要的是将所有结果添加到一个切片中,然后使用指向该切片的键“条目”编组一个映射或结构。
你的代码应该是这样的
groups := make([]ColorGroup, 0)
for i := 0; i < len(img.Entries); i++ {
address := img.Entries[i].Address
email1 := img.Entries[i].Email
hashedPassword := img.Entries[i].HashedPassword
deHashedPassword := "12233"
id := img.Entries[i].ID
iPAddress := img.Entries[i].IPAddress
name := img.Entries[i].Name
obtainedFrom := img.Entries[i].ObtainedFrom
password := img.Entries[i].Password
phone := img.Entries[i].Phone
username := img.Entries[i].Username
vin := img.Entries[i].Vin
success := img.Success
group := ColorGroup{
Address: address,
Email: email1,
HashedPassword: hashedPassword,
DeHashedPassword: deHashedPassword,
ID: id,
IPAddress: iPAddress,
Name: name,
ObtainedFrom: obtainedFrom,
Password: password,
Phone: phone,
Username: username,
Vin: vin,
Success: success,
}
groups = append(groups, group)
}
b, err := json.Marshal(map[string]interface{}{
"entries": groups,
})
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("New JSON\n", string(b))
还要更改 Marshaled 字段的命名,不要忘记json像这样用标签命名字段
type ColorGroup struct {
ID int `json:"id"`
Address string `json:"address"`
Email string `json:"email"`
HashedPassword string `json:"hashed_password"`
DeHashedPassword string `json:"de_hashed_password"`
IPAddress string `json:"ip_address"`
Name string `json:"name"`
ObtainedFrom string `json:"obtained_from"`
Password string `json:"password"`
Phone string `json:"phone"`
Username string `json:"username"`
Vin string `json:"vin"`
Success bool `json:"success"`
}
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报