亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有多個數據的 GoLang JSON 負載準備

具有多個數據的 GoLang JSON 負載準備

Go
慕勒3428872 2023-06-19 15:15:58
我想以下面給出的格式創建 JSON 負載。我想要一個準備給定格式的代碼或模式。{    transactiontype: 'DDDDD'    emailType: 'QQQQQQ'    template: {        templateUrl: 'xry.kk'        templateName: 'chanda'    }    date: [        {            UserId: 1            Name: chadnan        },        {            UserId: 2            Name: kkkkkk        }    ]}
查看完整描述

3 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

希望這可以幫助 :


type Template struct {

TemplateURL string `json:"templateUrl" param:"templateUrl"`

TemplateName string `json:"templateName" param:"templateName"`

}


type Date struct {

UserId string `json:"UserId" param:"UserId"`

Name string `json:"Name" param:"Name"`

}

type NameAny struct {

*Template

TransactionType string `json:"transactiontype" param:"transactiontype"`

EmailType string `json:"emailType" param:"emailType"`

Data []Date `json:"date" param:"date"`

}

Data, _ := json.Marshal(NameAny)

Json(c, string(Data))(w, r)


查看完整回答
反對 回復 2023-06-19
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

鑒于您的 JSON,Go 結構是:


type AutoGenerated struct {

? ? Transactiontype string `json:"transactiontype"`

? ? EmailType? ? ? ?string `json:"emailType"`

? ? Template? ? ? ? struct {

? ? ? ? TemplateURL? string `json:"templateUrl"`

? ? ? ? TemplateName string `json:"templateName"`

? ? } `json:"template"`

? ? Date []struct {

? ? ? ? UserID int? ? `json:"UserId"`

? ? ? ? Name? ?string `json:"Name"`

? ? } `json:"date"`

}

轉換后,使用json.Marshal?(Go Struct to JSON) 和json.Unmarshal?(JSON to Go Struct)

使用您的數據完成示例:https ://play.golang.org/p/RJuGK4cY1u-


查看完整回答
反對 回復 2023-06-19
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

// Transaction is a struct which stores the transaction details

type Transaction struct {

    TransactionType string   `json:"transaction_type"`

    EmailType       string   `json:"email_type"`

    Template        Template `json:"template"`

    Date            []Date   `json:"date"`

}


//Template is a struct which stores the template details

type Template struct {

    TemplateURL  string `json:"template_url"`

    TemplateName string `json:"template_name"`

}


// Date is a struct which stores the user details

type Date struct {

    UserID int    `json:"user_id"`

    Name   string `json:"name"`

}

上面給定的結構是用于存儲 json 主體的正確數據結構,您可以使用 json 解碼器將數據完美地存儲到結構中


func exampleHandler(w http.ResponseWriter, r *http.Request) {

    var trans Transaction

    decoder := json.NewDecoder(r.Body)

    err := decoder.Decode(&trans)

    if err != nil {

        log.Println(err)

    }

}



查看完整回答
反對 回復 2023-06-19
  • 3 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號