本文整理匯總了Golang中github.com/justintan/wine.Context.JSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.JSON方法的具體用法?Golang Context.JSON怎麽用?Golang Context.JSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/justintan/wine.Context
的用法示例。
在下文中一共展示了Context.JSON方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ResponseError
func ResponseError(c wine.Context, err ErrType) {
msg := ""
switch err {
case ErrParm:
msg = "parameter error"
case ErrServer:
msg = "server error"
default:
break
}
c.JSON(gox.M{"error": err, "msg": msg})
}
示例2: HandlePostDailyReport
func HandlePostDailyReport(c wine.Context) {
var req PostDailyReportRequest
req.UserId = c.Params().GetInt64("user_id")
req.Date = c.Params().GetStr("date")
req.Work = c.Params().GetStr("work")
req.Plan = c.Params().GetStr("plan")
req.Remark = c.Params().GetStr("remark")
if req.UserId < 0 || len(req.Date) == 0 {
c.JSON(gox.M{"error": 1})
return
}
values := url.Values{}
values.Set("user_id", strconv.FormatInt(req.UserId, 10))
values.Set("date", req.Date)
values.Set("work", req.Work)
values.Set("plan", req.Plan)
values.Set("remark", req.Remark)
resp, e := http.DefaultClient.PostForm("http://localhost:8001/daily-report", values)
if e != nil {
c.JSON(gox.M{"error": 2, "msg": "server error"})
return
}
data, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var respMap map[string]interface{}
if err := json.Unmarshal(data, &respMap); err == nil {
if err, ok := respMap["error"].(float64); ok && int(err) == 0 {
c.JSON(gox.M{"error": 0})
} else {
log.Println(respMap)
c.JSON(gox.M{"error": 1, "msg": "failed to add report"})
}
} else {
log.Println(respMap)
c.JSON(gox.M{"error": 2, "msg": "server error"})
}
}
示例3: ResponseData
func ResponseData(c wine.Context, data interface{}) {
c.JSON(gox.M{"error": ErrNone, "msg": "", "data": data})
}