第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何編組 JSON

如何編組 JSON

Go
胡子哥哥 2023-05-15 10:11:59
我正在嘗試使用 Go 以特定格式編組 JSON。我正在遍歷 JSON 并打印單個(gè)對(duì)象響應(yīng)。我想要的是根據(jù)一種格式存儲(chǔ)所有對(duì)象?,F(xiàn)在我卡住了,像這樣封送 JSON 結(jié)果:{     "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}
查看完整描述

1 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

您需要的是將所有結(jié)果添加到一個(gè)切片中,然后使用指向該切片的鍵“條目”編組一個(gè)映射或結(jié)構(gòu)。


你的代碼應(yīng)該是這樣的


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像這樣用標(biāo)簽命名字段


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

}


查看完整回答
反對(duì) 回復(fù) 2023-05-15
  • 1 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)