请告诉我如何将多维数组绑定到结构体中。

huangapple go评论95阅读模式
英文:

Please tell me how to bind multi-array to struct

问题

type _getData struct {
Title string json:"title" form:"title"
Date string json:"date" form:"date"
Pages []struct {
Order int json:"order" form:"title"
Description string json:"description" form:"description"
} json:"pages" form:"pages"
}

func CreateDiary(c echo.Context) error {

var getData _getData
c.Bind(&getData)
fmt.Print(getData)
...

通过c.FormParams命令接收以下数据,请告诉我如何将其绑定到_getData结构体中的pages字段。

map[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-05] location:[37.32779072192643 -122.01981157064436] map_id:[0] pages[0][description]:[123123] pages[0][order]:[0] pages[1][description]:[123123] pages[1][order]:[1] tags[0][id]:[12] tags[0][tag_name]:[sdf] title:[123123]]

我想将pages的数据作为数组获取,但是我得到的是[]

英文:
type _getData struct {
Title string `json:"title" form:"title"`
Date  string `json:"date" form:"date"`
Pages []struct {
	Order       int    `json:"order" form:"title"`
	Description string `json:"description" form:"description"`
} `json:"pages" form:"pages"`


func CreateDiary(c echo.Context) error {

var getData _getData
c.Bind(&getData)
fmt.Print(getData)
...

Receive the following data through c.FormParams command, please tell me how to bind it to _getData struct,

map[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-05] location:[37.32779072192643 -122.01981157064436] map_id:[0] pages[0][description]:[123123] pages[0][order]:[0] pages[1][description]:[123123] pages[1][order]:[1] tags[0][id]:[12] tags[0][tag_name]:[sdf] title:[123123]]

I want to get the data of pages as an array, but I am getting []

答案1

得分: 0

你可以使用第三方库。

import "github.com/monoculum/formam/v3"
type MyFormData struct {
    Pages []struct {
        Order       int    `formam:"order"`
        Description string `formam:"description"`
    } `formam:"pages"`
    Tags []struct {
        TagName string `formam:"tag_name"`
        Id      string `formam:"id"`
    } `formam:"tags"`
    Title string `formam:"title"`
}
func HttpHandler(c echo.Context) error {
    myFormData := MyFormData{}
    form, err := c.FormParams()

    if err != nil {
        return err
    }

    dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
    dec.Decode(form, &myFormData)

    return c.JSON(200, myFormData)
}
英文:

You can use 3rd party lib.

import "github.com/monoculum/formam/v3"
type MyFormData struct {
	Pages []struct {
		Order       int    `formam:"order"`
		Description string `formam:"description"`
	} `formam:"pages"`
	Tags []struct {
		TagName string `formam:"tag_name"`
		Id      string `formam:"id"`
	} `formam:"tags"`
	Title string `formam:"title"`
}
func HttpHandler(c echo.Context) error {
	myFormData := MyFormData{}
	form, err := c.FormParams()

	if err != nil {
		return err
	}

	dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
	dec.Decode(form, &myFormData)

	return c.JSON(200, myFormData)
}

huangapple
  • 本文由 发表于 2021年10月5日 14:17:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/69445675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定