douzhi8244 2017-02-01 09:01
浏览 322
已采纳

如何在Golang中将多个数据对象传递给HTML模板

I am returing all rows of a table as json to the variable pdata and unmarshaling it into an interface object. I have an instance of the user struct which I would like to pass along with the unmarshalled json data to the render function and access it using field arguments {{.fieldname}} in the html template.

if uuid != "" {
    pdata, err := getProduct()
    if err != nil {
        fmt.Println(err)
    } 

    type Prdata struct {
        Puid     string `json:"puid"`
        Pname    string `json:"pname"`
        Quantity string `json:"quantity"`
        Price    string `json:"price"`
        Image    string `json:"image"`
    }

    // Obj:= make(map[Prdata]string)

    var Pr Prdata

    err = json.Unmarshal(pdata , &Pr)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(string(pdata))
    fmt.Println(Pr)
    fmt.Println(u)

    render(w, "internal", Pr)
}

fmt.Println(string(pdata)) gives this output

[{"image":"1Appleiphone7.jpeg","pname":"iphone7","price":"70000","puid":"d6742e4e-2ad6-43c5-97f4-e8a7b00684e2","quantity":"100"}]

I have only been successful to unmarshal the data into an interface{} object. Trying to make maps with keys of the type interface{} and values of type string but it throws the error:

"json: cannot unmarshal array into Go value of type map[interface {}]string"

The render function takes an argument of the type interface{}

func render(w http.ResponseWriter, name string, data interface{})

fmt.Println(Pr) gives this output:

[map[quantity:100 image:1Appleiphone7.jpeg pname:iphone7 price:70000 puid:d6742e4e-2ad6-43c5-97f4-e8a7b00684e2]]

u is an instance of struct User

var u = &User{}

type User struct {
    Uuid     string            
    Username string           
    Password string           
    Fname    string            
    Email    string            
}

I can see the output on the html page using the pipeline {{.}}. However I am not able to access any data using the fieldname.

There must be a way of doing this. But I am not sure how?

When I pass of a json of the type below, I am able to pass it to the struct type and reference it by the key values using pipelines in my template.

    str := `{
    "image": "1Appleiphone7.jpeg",
    "pname": "iphone7",
    "price": "70000",
    "puid": "d6742e4e-2ad6-43c5-97f4-e8a7b00684e2",
    "quantity": "100"
    }`

unmarshal function

err = json.Unmarshal([]byte(str), &Pr)

The difference in the json data of the DB record pdata and the one above str is in the use of backticks "`". It seems that though the json data shows key value pairs, it is actually a json array and not a json object. Is there a way to get around this?

  • 写回答

2条回答 默认 最新

  • douliexu5623 2017-02-04 09:36
    关注

    I am posting a working example of unmarshalling json as bytes into a struct type which then can be referenced using the {{.}} in the template.

    package main

    import (
        "encoding/json"
        "fmt"
    )
    
    type Usrdata struct {
        Uuid  string
        Fname string
    }
    
    type Prdata struct {
        Puid     string `json:"puid"`
        Pname    string `json:"pname"`
        Quantity string `json:"quantity"`
        Price    string `json:"price"`
        Image    string `json:"image"`
    }
    
    type Data struct {
        U Usrdata
        P []Prdata
    }
    
    func main() {
        Ur := Usrdata{Uuid: "xyz", Fname: "Somename"}
        Pr := make([]Prdata, 0)
        var Dt Data
        Dt.U = Ur
        pdata := `[{"image":"1Appleiphone7.jpeg","pname":"iphone7","price":"70000","puid":"d6742e4e-2ad6-43c5-97f4-e8a7b00684e2","quantity":"100"}]`
        err := json.Unmarshal([]byte(pdata), &Pr)
        if err != nil {
            fmt.Println(err)
        }
        Dt.P = Pr
        fmt.Println(Pr[0].Pname)
        fmt.Println(Pr)
        fmt.Println(Dt)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载