猿问

如何发送地图数组并使用 gin 模板对其进行迭代

以下是工作代码的片段。我正在使用杜松子酒模板引擎。


c.HTML(200, "index", gin.H{

            "title":    "Welcome",

            "students": map[int]map[string]string{1: {"PID": "1", "Name": "myName"}},})

在索引模板中我有:


 <TABLE class= "myTable" >

        <tr class="headingTr">

            <td>Name</td>

        </tr>

        {{range $student := .students}}

        <td>{{$student.Name}}</td>                    

        {{end}}

 </TABLE>

如您所见,我已经硬编码了students标题(地图)上的值。我想从我构建的休息 API 中获取这些数据。我的 rest API 的响应是一个数组:


 [

  {

    "id": 1,

    "name": "Mary"

  },

  {

    "id": 2,

    "name": "John"

  }

]

我可以将此 JSON 响应解组为map[string]string而不是map[int]map[string]string. 如何将这个未编组的主体传递给学生的参数值,然后在这个数组上迭代索引模板?


慕桂英3389331
浏览 177回答 1
1回答

肥皂起泡泡

带结构你所拥有的是一个 JSON 数组,将它解组为一个 Go 切片。建议创建一个Student结构来为您的学生建模,以获得干净且有意识的 Go 代码。并且在模板中,{{range}}动作将点.设置为当前元素,您可以在{{range}}正文中将其简单地称为点.,因此学生姓名将是.Name。工作代码(在Go Playground上试试):func main() {&nbsp; &nbsp; t := template.Must(template.New("").Parse(templ))&nbsp; &nbsp; var students []Student&nbsp; &nbsp; if err := json.Unmarshal([]byte(jsondata), &students); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; params := map[string]interface{}{"Students": students}&nbsp; &nbsp; if err := t.Execute(os.Stdout, params); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(nil)&nbsp; &nbsp; }}const jsondata = `[&nbsp; {&nbsp; &nbsp; "id": 1,&nbsp; &nbsp; "name": "Mary"&nbsp; },&nbsp; {&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "name": "John"&nbsp; }]`const templ = `<TABLE class= "myTable" >&nbsp; &nbsp; &nbsp; &nbsp; <tr class="headingTr">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Name</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; {{range .Students}}&nbsp; &nbsp; &nbsp; &nbsp; <td>{{.Name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp;</TABLE>`输出:<TABLE class= "myTable" >&nbsp; &nbsp; &nbsp; &nbsp; <tr class="headingTr">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Name</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>Mary</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <td>John</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;</TABLE>带地图如果您不想创建和使用Student结构,您仍然可以使用类型为的简单映射来完成,该映射map[string]interface{}可以表示任何 JSON 对象,但要知道在这种情况下您必须按.name原样引用学生的姓名它在 JSON 文本中的显示方式,因此"name"将在未编组的 Go 映射中使用小写键:func main() {&nbsp; &nbsp; t := template.Must(template.New("").Parse(templ))&nbsp; &nbsp; var students []map[string]interface{}&nbsp; &nbsp; if err := json.Unmarshal([]byte(jsondata), &students); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; params := map[string]interface{}{"Students": students}&nbsp; &nbsp; if err := t.Execute(os.Stdout, params); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(nil)&nbsp; &nbsp; }}const templ = `<TABLE class= "myTable" >&nbsp; &nbsp; &nbsp; &nbsp; <tr class="headingTr">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Name</td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; {{range .Students}}&nbsp; &nbsp; &nbsp; &nbsp; <td>{{.name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp;</TABLE>`输出是一样的。在Go Playground上试试这个变体。
随时随地看视频慕课网APP

相关分类

Go
我要回答