本文整理汇总了Golang中github.com/martini-contrib/render.Render.JSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Render.JSON方法的具体用法?Golang Render.JSON怎么用?Golang Render.JSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/martini-contrib/render.Render
的用法示例。
在下文中一共展示了Render.JSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AdminGetModels
func AdminGetModels(params martini.Params, r render.Render) {
var itemList []string
for k, _ := range models {
itemList = append(itemList, k)
}
r.JSON(200, map[string]interface{}{"models": itemList})
}
示例2: MediaCreate
func MediaCreate(db gorm.DB, r render.Render, media models.Media) {
if err := db.Save(&media).Error; err != nil {
r.JSON(http.StatusConflict, map[string]interface{}{"error": "Media conflict"})
return
}
r.JSON(http.StatusCreated, media)
}
示例3: ReadListQnA
func ReadListQnA(req *http.Request, r render.Render, db *mgo.Database) {
appid := req.Header.Get("Application-Id")
if appid == "" {
r.JSON(handlers.HttpErr(http.StatusNotFound, "insert to Application-Id"))
return
}
////
colQuerier := bson.M{}
change := bson.M{"$set": bson.M{"reception": true}}
CollectionName := handlers.CollectionNameQnA(appid)
if _, err := db.C(CollectionName).UpdateAll(colQuerier, change); err != nil {
r.JSON(handlers.HttpErr(http.StatusNotFound, err.Error()))
return
}
var qnas []QnA
if err := db.C(CollectionName).Find(bson.M{}).Sort("-time").All(&qnas); err != nil {
r.JSON(handlers.HttpErr(http.StatusNotFound, err.Error()))
return
}
if qnas == nil {
r.JSON(http.StatusOK, map[string]interface{}{"return": bson.D{}})
return
}
r.JSON(http.StatusOK, map[string]interface{}{"return": qnas})
}
示例4: ListBuckets
func ListBuckets(r render.Render, db database.Database) {
if buckets, err := db.ListBuckets(); err != nil {
JsonErrorf(r, http.StatusBadRequest, err.Error())
} else {
r.JSON(http.StatusOK, buckets)
}
}
示例5: GetAllRelations
//Get /users/:user_id/relationships
func (u *UserHandler) GetAllRelations(request *http.Request, rd render.Render, params martini.Params) {
// 获取 url 参数
userid, err := strconv.ParseInt(params["user_id"], 10, 64)
if err != nil {
rd.Text(400, "The request cannot be fulfilled due to bad syntax.")
return
}
//验证 user 是否存在
_, err = u.userService.GetUser(userid)
if err != nil {
if err == pg.ErrNoRows {
rd.Text(400, fmt.Sprintln("user", userid, "not exist"))
} else {
rd.Text(500, err.Error())
}
return
}
rs, err := u.userService.GetUserRelations(userid)
if err != nil {
rd.Text(500, err.Error())
return
}
relations := make([]interface{}, 0, 0)
for _, r := range rs {
relations = append(relations, fmtRelationShip(r))
}
rd.JSON(200, relations)
}
示例6: CollectionResponse
func CollectionResponse(r render.Render, collection []interface{}, err error) {
if err != nil {
r.JSON(404, nil)
} else {
r.JSON(200, collection)
}
}
示例7: createUser
func createUser(r render.Render, re *http.Request, f *fishhub.DBService, userForm user.UserForm) {
userExistError := binding.Error{
Message: "is already taken",
FieldNames: []string{"email"},
Classification: "UserExistError",
}
errors := binding.Errors{userExistError}
if userExist(f, userForm.UserId) {
r.JSON(400, errors)
return
}
d := f.DB.Copy()
defer d.Close()
query := bson.M{"userid": userForm.UserId}
update := bson.M{"$set": userForm}
updated, _ := d.Upsert("users", query, nil, update, true)
if updated == true {
r.JSON(200, map[string]interface{}{
"message": "User profile is successfully created.",
})
return
}
displayUnknownError(r)
}
示例8: Vote
func Vote(db model.Model, req *http.Request, r render.Render, params martini.Params) {
id, err := strconv.Atoi(params["id"])
if err != nil {
r.JSON(404, ErrorEnv{"invalid quote id"})
return
}
count, err := strconv.Atoi(req.FormValue("count"))
if err != nil {
r.JSON(404, ErrorEnv{"invalid vote count"})
return
}
err = db.VoteQuote(id, count)
if err != nil {
r.JSON(500, ErrorEnv{"unable to vote quote"})
return
}
quote, err := db.GetQuote(id)
if err != nil {
r.JSON(404, ErrorEnv{"quote not found"})
return
}
env := struct {
Score int `json:"score"`
}{quote.Score}
r.JSON(200, env)
}
示例9: Index
func Index(r render.Render) {
type homeStats struct {
RepoCount int
IssueCount int
UserCount int
IssueLangs []string
}
stats := homeStats{}
c.DB.Model(c.Repository{}).Count(&stats.RepoCount)
c.DB.Model(c.Issue{}).Count(&stats.IssueCount)
c.DB.Model(c.User{}).Count(&stats.UserCount)
rows := c.DB.Raw(`select distinct language
from issues
inner join repositories on repositories.id = issues.repository_id
where language != ''
order by language
`)
rows.Pluck("language", &stats.IssueLangs)
r.JSON(200, stats)
}
示例10: Random
func Random(db model.Model, r render.Render, req *http.Request, isJson IsJson) {
qs := req.URL.Query()
search := qs.Get("query")
maxLines, err := strconv.Atoi(qs.Get("max-lines"))
if err != nil || maxLines < 1 {
maxLines = 0
}
quotes, err := db.GetQuotes(model.Query{
Limit: 1,
Search: search,
MaxLines: maxLines,
OrderBy: []string{"rand()"},
IncludeTags: qs["tags"],
ExcludeTags: qs["exclude-tags"],
})
if err != nil || len(quotes) == 0 {
RenderError(r, 500, isJson, fmt.Sprint("quote not found", err))
return
}
quote := quotes[0]
if isJson {
r.JSON(200, quote)
} else {
r.Redirect(fmt.Sprintf("/quote/%d", quote.ID))
}
}
示例11: LoginHandler
func LoginHandler(r render.Render, user model.User, appx *appx.Datastore) {
response := model.Response{
ErrorCode: http.StatusOK,
Message: []string{},
Data: nil,
}
isValid, validationErr := user.IsValid()
if !isValid {
response.ErrorCode = http.StatusBadRequest
response.Message = validationErr
} else {
err := appx.Save(&user)
if err != nil {
log.Printf("Error: %v", err)
response.ErrorCode = http.StatusInternalServerError
response.Message = append(response.Message, err.Error())
} else {
response.Data = user
}
}
r.JSON(200, response)
}
示例12: PostIdeaHandler
func PostIdeaHandler(req *http.Request, render render.Render, log *log.Logger) {
log.Println("Receive post data.")
name := req.FormValue("name")
email := req.FormValue("email")
link := req.FormValue("link")
content := req.FormValue("content")
log.Println("Name: " + name)
log.Println("Email: " + email)
log.Println("Link: " + link)
log.Println("Content: " + content)
if len(name) == 0 {
render.JSON(400, map[string]interface{}{"error": 1, "message": "Empty name."})
return
}
if len(email) == 0 {
render.JSON(400, map[string]interface{}{"error": 2, "message": "Empty email."})
return
}
if len(content) == 0 {
render.JSON(400, map[string]interface{}{"error": 3, "message": "Empty content."})
return
}
AddIdea(name, email, link, content)
render.JSON(200, map[string]interface{}{"error": 0})
}
示例13: request
func (this *EurekaRequestGet) request(data interface{}, r render.Render, req *http.Request) {
if req.URL.Query().Get("json") != "" {
r.JSON(200, data)
} else {
r.XML(200, data)
}
}
示例14: ListBuckets
func ListBuckets(ctx context.Context, r render.Render, db database.Database) {
if buckets, err := db.ListBuckets(); err != nil {
LogAndRespondWithError(ctx, r, http.StatusBadRequest, err)
} else {
r.JSON(http.StatusOK, buckets)
}
}
示例15: UpdateQuestion
/**
更新
**/
func UpdateQuestion(r render.Render, req *http.Request) {
c := appengine.NewContext(req)
id, _ := strconv.Atoi(req.FormValue("Key"))
key := datastore.NewKey(c, "Question", "", int64(id), nil)
var question Question
if err := datastore.Get(c, key, &question); err != nil {
c.Criticalf(err.Error())
}
question.Content = req.FormValue("Content")
question.LargeCategoryKey = req.FormValue("LargeCategoryKey")
question.MediumCategoryKey = req.FormValue("MediumCategoryKey")
question.SmallCategoryKey = req.FormValue("SmallCategoryKey")
question.Rubric = req.FormValue("Rubric")
question.Percentage, _ = strconv.ParseFloat(req.FormValue("Percentage"), 64)
question.Status = req.FormValue("Status")
question.Level = req.FormValue("Level")
question.UserKey = req.FormValue("UserKey")
_, err := datastore.Put(c, key, &question)
if err != nil {
c.Criticalf("%s", err)
r.JSON(400, err)
} else {
r.JSON(200, question)
}
}