如何编组 JSON

我正在尝试使用 Go 以特定格式编组 JSON。我正在遍历 JSON 并打印单个对象响应。我想要的是根据一种格式存储所有对象。现在我卡住了,像这样封送 JSON 结果:


{  

   "Address":null,

   "Email":"abc@hotmail.com",

   "HashedPassword":"4233137d1c510f2e55ba5cb220b864b11033f156",

   "DeHashedPassword":"123456",

   "ID":"Gd0YhYEJdE6oejsjBm7xLTQ4lWIaRecbS-k=",

   "IPAddress":null,

   "Name":null,

   "ObtainedFrom":"LinkedIn",

   "Password":null,

   "Phone":null,

   "Username":null,

   "Vin":null,

   "Success":true

}{  

   "Address":"",

   "Email":"abc@hotmail.com",

   "HashedPassword":"",

   "DeHashedPassword":"123456",

   "ID":"Jge4Mm6M-5-yJedG2ql48M9H2p7qP83aggM=",

   "IPAddress":"",

   "Name":"",

   "ObtainedFrom":"DailyMotion.com",

   "Password":"dm_51978c5a67a88",

   "Phone":"",

   "Username":"",

   "Vin":"",

   "Success":true

}{  

   "Address":"",

   "Email":"abc@hotmail.com",

   "HashedPassword":"",

   "DeHashedPassword":"123456",

   "ID":"9k8llNeinyrmxhL7yg3zZ50rQiQk_BmzZS8=",

   "IPAddress":"",

   "Name":"",

   "ObtainedFrom":"BreachCompilation",

   "Password":"hello123",

   "Phone":"",

   "Username":"",

   "Vin":"",

   "Success":true

}

我想要得到的是像这样编组 json


{

"entries": [

{

"id": "CHzLLBdoJiwd7WaySw8QBOoxkj2lmKFhJK8=",

"email": "abc@hotmail.com",

"username": null,

"password": null,

"hashed_password": "4233137d1c510f2e55ba5cb220b864b11033f156",

"name": null,

"vin": null,

"address": null,

"ip_address": null,

"phone": null,

"obtained_from": "LinkedIn"

},

{

"id": "O6W3lxVMo_faf7MWoGGgkMb_CGcjo5vinFQ=",

"email": "abc@hotmail.com",

"username": "",

"password": "dm_51978c5a67a88",

"hashed_password": "",

"name": "",

"vin": "",

"address": "",

"ip_address": "",

"phone": "",

"obtained_from": "DailyMotion.com"

}


],

"success": true

}


胡子哥哥
浏览 88回答 1
1回答

慕沐林林

您需要的是将所有结果添加到一个切片中,然后使用指向该切片的键“条目”编组一个映射或结构。你的代码应该是这样的groups := make([]ColorGroup, 0)for i := 0; i < len(img.Entries); i++ {&nbsp; &nbsp; address := img.Entries[i].Address&nbsp; &nbsp; email1 := img.Entries[i].Email&nbsp; &nbsp; hashedPassword := img.Entries[i].HashedPassword&nbsp; &nbsp; deHashedPassword := "12233"&nbsp; &nbsp; id := img.Entries[i].ID&nbsp; &nbsp; iPAddress := img.Entries[i].IPAddress&nbsp; &nbsp; name := img.Entries[i].Name&nbsp; &nbsp; obtainedFrom := img.Entries[i].ObtainedFrom&nbsp; &nbsp; password := img.Entries[i].Password&nbsp; &nbsp; phone := img.Entries[i].Phone&nbsp; &nbsp; username := img.Entries[i].Username&nbsp; &nbsp; vin := img.Entries[i].Vin&nbsp; &nbsp; success := img.Success&nbsp; &nbsp; group := ColorGroup{&nbsp; &nbsp; &nbsp; &nbsp; Address:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; address,&nbsp; &nbsp; &nbsp; &nbsp; Email:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email1,&nbsp; &nbsp; &nbsp; &nbsp; HashedPassword:&nbsp; &nbsp;hashedPassword,&nbsp; &nbsp; &nbsp; &nbsp; DeHashedPassword: deHashedPassword,&nbsp; &nbsp; &nbsp; &nbsp; ID:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id,&nbsp; &nbsp; &nbsp; &nbsp; IPAddress:&nbsp; &nbsp; &nbsp; &nbsp; iPAddress,&nbsp; &nbsp; &nbsp; &nbsp; Name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name,&nbsp; &nbsp; &nbsp; &nbsp; ObtainedFrom:&nbsp; &nbsp; &nbsp;obtainedFrom,&nbsp; &nbsp; &nbsp; &nbsp; Password:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;password,&nbsp; &nbsp; &nbsp; &nbsp; Phone:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; phone,&nbsp; &nbsp; &nbsp; &nbsp; Username:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;username,&nbsp; &nbsp; &nbsp; &nbsp; Vin:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vin,&nbsp; &nbsp; &nbsp; &nbsp; Success:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success,&nbsp; &nbsp; }&nbsp; &nbsp; groups = append(groups, group)}b, err := json.Marshal(map[string]interface{}{&nbsp; &nbsp; "entries": groups,})if err != nil {&nbsp; &nbsp; fmt.Println("error:", err)}fmt.Println("New JSON\n", string(b))还要更改 Marshaled 字段的命名,不要忘记json像这样用标签命名字段type ColorGroup struct {&nbsp; &nbsp; ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int&nbsp; &nbsp; `json:"id"`&nbsp; &nbsp; Address&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"address"`&nbsp; &nbsp; Email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"email"`&nbsp; &nbsp; HashedPassword&nbsp; &nbsp;string `json:"hashed_password"`&nbsp; &nbsp; DeHashedPassword string `json:"de_hashed_password"`&nbsp; &nbsp; IPAddress&nbsp; &nbsp; &nbsp; &nbsp; string `json:"ip_address"`&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"name"`&nbsp; &nbsp; ObtainedFrom&nbsp; &nbsp; &nbsp;string `json:"obtained_from"`&nbsp; &nbsp; Password&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"password"`&nbsp; &nbsp; Phone&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"phone"`&nbsp; &nbsp; Username&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string `json:"username"`&nbsp; &nbsp; Vin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `json:"vin"`&nbsp; &nbsp; Success&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool&nbsp; &nbsp;`json:"success"`}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go