如何封送JSON

时间:2018-11-29 08:23:26

标签: json go marshalling

我正在尝试使用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
}

代码在Go中:

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,
    }

    b, err := json.Marshal(group)
    if err != nil {
        fmt.Println("error:", err)
    }
    ab := string(b)
    fmt.Println("New json", ab)
}

1 个答案:

答案 0 :(得分:1)

您需要的是将所有结果添加到切片中,然后使用指向该切片的键“ entries”编组地图或结构。

您的代码应如下所示

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))

还可以更改封送处理字段的命名,不要忘记像这样用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"`
}