3 回答

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
希望這可以幫助 :
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)

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
鑒于您的 JSON,Go 結(jié)構(gòu)是:
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"`
}
轉(zhuǎn)換后,使用json.Marshal?(Go Struct to JSON) 和json.Unmarshal?(JSON to Go Struct)
使用您的數(shù)據(jù)完成示例:https ://play.golang.org/p/RJuGK4cY1u-

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
// 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"`
}
上面給定的結(jié)構(gòu)是用于存儲(chǔ) json 主體的正確數(shù)據(jù)結(jié)構(gòu),您可以使用 json 解碼器將數(shù)據(jù)完美地存儲(chǔ)到結(jié)構(gòu)中
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)
}
}
- 3 回答
- 0 關(guān)注
- 145 瀏覽
添加回答
舉報(bào)