json - 如何编码 JSON

标签 json go marshalling

我正在尝试使用 Go 以特定格式编码 JSON。我正在遍历 JSON 并打印单个对象响应。我想要的是根据一种格式存储所有对象。现在我卡住了,像这样编码(marshal) 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)
}

最佳答案

您需要的是将所有结果添加到一个 slice ,然后使用指向该 slice 的键“条目”编码一个映射或结构。

你的代码应该是这样的

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"`
}

关于json - 如何编码 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534597/

相关文章:

javascript - 使用 javascript 显示测验问题

javascript - 将 JSON 对象重新映射到其他 JSON 结构

java - JAXB 绑定(bind) 3 级嵌套元素

c# - 列表和字符串转换

java - 将 json 数据传递给 spring-boot 应用程序的 Curl POST 命令不起作用

postgresql - 如何从go app查看postgresql "raise notice"

go - 计算数组中的不同值 - 性能提示

reactjs - 如何通过react/axios和golang/gin上传图片到S3

c++ - 请帮助我将这个结构数组编码为 C++

javascript - 使用jquery根据用户点击的链接id动态创建文件路径