golang 数据建模json解析

1.0 controller get请求

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视图输出数据

        <div class="ads">
         {{range .Myad}}
         <a href="{{.Url}}"> <img src="/{{.Uploadfiles}}" width="{{.Width}}" height="{{.Height}}"  alt="{{.Title}}" /></a>
        {{end}}
        </div>

 

posted @ 2014-08-12 17:58  开心编码源  阅读(643)  评论(0编辑  收藏  举报