当前位置: > > > > 如何解析表单帖子中的数组
来源:stackoverflow
2024-04-27 20:24:57
0浏览
收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天就整理分享《如何解析表单帖子中的数组》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
问题内容
我目前有一个表单帖子即将发布
{ "stuff":"cool text", "otherthing":"neat thing", "captions":[ {"first":"the list", "second":"how are you"}, {"first":"wow", etc.... ] }
现在我不知道会有多少字幕。它可能是数组中的一个,也可能是二十个。
我也设置了两个结构
type thingcontext struct { stuff string `json:"stuff"` otherthing string `json:"otherthing"` captions []arraytext `json:"captions"` } type arraytext struct { first string `json:"first"` second string `json:"second"` }
在我的 golang 函数中我有这样的东西
func (c *thingcontext) setthingcontext(rw web.responsewriter, req *web.request, next web.nextmiddlewarefunc) { if err := req.parseform(); err != nil { } c.stuff = req.formvalue("stuff") c.otherthing = req.formvalue("otherthing") }
在我尝试解析数组之前,这一切正常。 当我按照 c.captions = req.parseform("captions")
进行操作时 我收到错误
.cannot use req.Request.ParseForm("captions") (type error) as type []ArrayText in assignment
解决方案
除了作业之外,你做得对。当您运行 req.request.parseform() 时,它不会返回值或传递对缓冲区的引用,而是填充 req.request.form 和 req.request.postform 结构。
所以而不是
c.captions = req.request.parseform()
看起来更像
err := req.Request.ParseForm() //check for errors as usual here c.Captions = req.Request.Form //or c.Captions = req.Request.PostForm
从这个方向接近它应该会让你走上正确的道路。
干杯! 泰勒
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何解析表单帖子中的数组》文章吧,也可关注公众号了解相关技术文章。