golang 數據建模json解析

1.0 controller get請求json

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {

    this.Data["IsHome"] = true
    this.Data["Website"] = "s.me"
    this.Data["Email"] = "304030196@qq.com"
    this.TplNames = "templets/default/index.tpl"
    //根據路由判斷zoneid
    zoneid := getZone()
    //幻燈片模塊
    myads := getAds(zoneid)
    // this.Data["Myads"] = myads

    var normbody models.Normbody
    var data []models.Normbody
    for i := 0; i < len(myads); i++ {
        myad := myads[i]
        jsonStr := myad.Normbody
        if err := json.Unmarshal([]byte(jsonStr), &normbody); err == nil {
            fmt.Println("%v,%d", normbody, i)
            data = append(data, normbody)
        }

    }
    this.Data["Myad"] = data
    fmt.Println("%v", data)

}

/*************1.0 得到Zoneid*******************/
func getZone() string {

    return "2"
}

/*************2.0 得到廣告數據*******************/
func getAds(zoneid string) []models.Myads {
    datalist, err := models.GetMyads(zoneid)
    if err != nil {
        beego.Error(err)
        return nil
    }
    return datalist
}

2.0  view視圖輸出數據app

        <div class="ads">
         {{range .Myad}}
         <a href="{{.Url}}"> <img src="/{{.Uploadfiles}}" width="{{.Width}}" height="{{.Height}}"  alt="{{.Title}}" /></a>
        {{end}}
        </div>
相關文章
相關標籤/搜索