英文:
How to parse form array in golang Beego
问题
如何使用Beego解析HTML表单数组。
<input name="names[]" type="text" />
<input name="names[]" type="text" />
<input name="names[]" type="text" />
Go Beego
type Rsvp struct {
Id int `form:"-"`
Names []string `form:"names[]"`
}
rsvp := Rsvp{}
if err := this.ParseForm(&rsvp); err != nil {
//处理错误
}
input := this.Input()
fmt.Printf("%+v\n", input) // map[names[]:[name1 name2 name3]]
fmt.Printf("%+v\n", rsvp) // {Names:[]}
为什么Beego的ParseForm方法返回一个空的names?
如何将值获取到rsvp.Names中?
英文:
How to parse html form array with Beego.
<!-- language-all: lang-html -->
<input name="names[]" type="text" />
<input name="names[]" type="text" />```
Go Beego
type Rsvp struct {
Id int `form:"-"`
Names []string `form:"names[]"`
}
rsvp := Rsvp{}
if err := this.ParseForm(&rsvp); err != nil {
//handle error
}
input := this.Input()
fmt.Printf("%+v\n", input) // map[names[]:[name1 name2 name3]]
fmt.Printf("%+v\n", rsvp) // {Names:[]}
Why Beego ParseForm method return an empty names?
How to get values into rsvp.Names?
</details>
# 答案1
**得分**: 6
感谢 @ysqi 给了我一个提示。我正在添加一个更详细的示例来解析类似于beego中的关联数组形式的表单数据。
这是我的表单结构:
<input name="contacts[0][email]" type="text" value="a1@gmail.com"/>
<input name="contacts[0][first_name]" type="text" value="f1"/>
<input name="contacts[0][last_name]" type="text" value="l1"/>
<input name="contacts[1][email]" type="text" value="a2@gmail.com"/>
<input name="contacts[1][first_name]" type="text" value="f2"/>
<input name="contacts[1][last_name]" type="text" value="l2"/>
golang(beego) 代码:
contacts := make([]map[string]string, 0, 3)
this.Ctx.Input.Bind(&contacts, "contacts")
contacts 变量:
[
{
"email": "user2@gmail.com",
"first_name": "Sam",
"last_name": "Gamge"
},
{
"email": "user3@gmail.com",
"first_name": "john",
"last_name": "doe"
}
]
现在你可以像这样使用它:
for _, contact := range contacts {
contact["email"]
contact["first_name"]
contact["last_name"]
}
<details>
<summary>英文:</summary>
Thanks @ysqi for giving me a hint. I am adding a little detailed example to parse associate array like form data in beego
Here is my form structure:
<input name="contacts[0][email]" type="text" value="a1@gmail.com"/>
<input name="contacts[0][first_name]" type="text" value="f1"/>
<input name="contacts[0][last_name]" type="text" value="l1"/>
<input name="contacts[1][email]" type="text" value="a2@gmail.com"/>
<input name="contacts[1][first_name]" type="text" value="f2"/>
<input name="contacts[1][last_name]" type="text" value="l2"/>
golang(beego) code:
contacts := make([]map[string]string, 0, 3)
this.Ctx.Input.Bind(&contacts, "contacts")
contacts variable:
[
{
"email": "user2@gmail.com",
"first_name": "Sam",
"last_name": "Gamge"
},
{
"email": "user3@gmail.com",
"first_name": "john",
"last_name": "doe"
}
]
Now you can use it like:
for _, contact := range contacts {
contact["email"]
contact["first_name"]
contact["last_name"]
}
</details>
# 答案2
**得分**: 4
从Request的FormValue方法的实现中可以看出,如果有多个值,它会返回第一个值:
http://golang.org/src/pkg/net/http/request.go?s=23078:23124#L795
最好的做法是直接获取属性r.Form[key],然后手动遍历所有的结果。我不确定Beego是如何工作的,但是只使用原始的Request.ParseForm和Request.Form或Request.PostForm映射应该可以完成任务。
http://golang.org/src/pkg/net/http/request.go?s=1939:6442#L61
<details>
<summary>英文:</summary>
As you can see from the implementation of the FormValue method of the Request, it returns the first value in case of multiple ones:
http://golang.org/src/pkg/net/http/request.go?s=23078:23124#L795
It would be better to get the attribute itself r.Form[key] and iterate over all the results manually. I am not sure how Beego works, but just using the raw
Request.ParseForm and Request.Form or Request.PostForm maps should do the job.
http://golang.org/src/pkg/net/http/request.go?s=1939:6442#L61
</details>
# 答案3
**得分**: 2
你可以这样做:参见[文档](http://beego.me/docs/mvc/controller/params.md)
v := make([]string, 0, 3)
this.Ctx.Input.Bind(&v, "names")
<details>
<summary>英文:</summary>
You can do like this : see [doc](http://beego.me/docs/mvc/controller/params.md)
v := make([]string, 0, 3)
this.Ctx.Input.Bind(&v, "names")
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论