doujiu8178 2018-11-29 08:23
浏览 62
已采纳

如何封送JSON

I am trying to marshal JSON in a specific format using Go. I am looping over the JSON and printing the individual object response. What I want is to store all the objects according to a format. Right now I am stuck and marshaling JSON results like this:

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

What I want to get is to marshal json like this

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

Code is here in 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条回答 默认 最新

  • dsxrq28228 2018-11-29 08:43
    关注

    What you need is adding all the results to a slice, then marshal a map or struct with the key "entries" pointing to that slice.

    Your code should look like this

    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
    ", string(b))
    

    Also to change the naming of the Marshaled fields, don't forget to name the fields with json tag like so

    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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码