【问题标题】:How to send an array of maps and iterate over it using gin-templating如何发送地图数组并使用 gin-template 对其进行迭代
【发布时间】:2016-08-23 16:57:19
【问题描述】:

以下是工作代码的 sn-p。我正在使用 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。如何将这个未编组的主体传递给学生的​​参数值,然后在这个数组中迭代索引模板?

【问题讨论】:

    标签: json go unmarshalling go-templates go-gin


    【解决方案1】:

    带有结构

    您拥有的是一个 JSON 数组,将其解组为 Go 切片。建议创建一个 Student 结构来为您的学生建模,以获得干净和有意识的 Go 代码。

    在模板中,{{range}} 操作将点 . 设置为当前元素,您可以在 {{range}} 正文中将其引用为点 .,因此学生姓名将是 @987654328 @。

    工作代码(在Go Playground 上试用):

    func main() {
        t := template.Must(template.New("").Parse(templ))
        var students []Student
        if err := json.Unmarshal([]byte(jsondata), &students); err != nil {
            panic(err)
        }
        params := map[string]interface{}{"Students": students}
        if err := t.Execute(os.Stdout, params); err != nil {
            panic(nil)
        }
    }
    
    const jsondata = `[
      {
        "id": 1,
        "name": "Mary"
      },
      {
        "id": 2,
        "name": "John"
      }
    ]`
    
    const templ = `<TABLE class= "myTable" >
            <tr class="headingTr">
                <td>Name</td>
            </tr>
            {{range .Students}}
            <td>{{.Name}}</td>                    
            {{end}}
     </TABLE>`
    

    输出:

    <TABLE class= "myTable" >
            <tr class="headingTr">
                <td>Name</td>
            </tr>
    
            <td>Mary</td>                    
    
            <td>John</td>                    
    
     </TABLE>
    

    有地图

    如果您不想创建和使用 Student 结构,您仍然可以使用类型为 map[string]interface{} 的简单映射来完成它,它可以表示任何 JSON 对象,但要知道在这种情况下您必须将学生的姓名称为 .name,因为它在 JSON 文本中是这样显示的,因此小写的 "name" 键将在未编组的 Go 映射中使用:

    func main() {
        t := template.Must(template.New("").Parse(templ))
        var students []map[string]interface{}
        if err := json.Unmarshal([]byte(jsondata), &students); err != nil {
            panic(err)
        }
        params := map[string]interface{}{"Students": students}
        if err := t.Execute(os.Stdout, params); err != nil {
            panic(nil)
        }
    }
    
    const templ = `<TABLE class= "myTable" >
            <tr class="headingTr">
                <td>Name</td>
            </tr>
            {{range .Students}}
            <td>{{.name}}</td>                    
            {{end}}
     </TABLE>`
    

    输出是一样的。在Go Playground 上试试这个变体。

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2021-05-01
      • 2021-11-23
      相关资源
      最近更新 更多