本文整理匯總了Golang中github.com/codegangsta/martini-contrib/render.Render.JSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang Render.JSON方法的具體用法?Golang Render.JSON怎麽用?Golang Render.JSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/codegangsta/martini-contrib/render.Render
的用法示例。
在下文中一共展示了Render.JSON方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: HandleCreateIRLMoji
func HandleCreateIRLMoji(r render.Render, bindErr binding.Errors, im models.IRLMoji, db *models.DB, backchannel Backchannel) {
if bindErr.Count() > 0 {
r.JSON(400, JsonErrBinding(bindErr))
return
}
if backchannel.UserId() == "" {
r.JSON(403, JsonErr("The provided credentials were invalid."))
return
}
user, err := db.GetUserWithId(backchannel.UserId())
if err != nil {
r.JSON(403, "Could not find a user with your credentials.")
return
}
// Now let's create that user, shall we?
insertedIM, err := db.InsertIM(user.Id, im.Emoji, im.Picture)
if err != nil {
log.Println("Error creating user:", err.Error())
r.JSON(500, JsonErr("Sorry, an internal server error has occurred."))
return
}
r.JSON(200, map[string]*models.IRLMoji{"irlmoji": insertedIM})
}
示例2: HandleRandomJsonTip
func HandleRandomJsonTip(r render.Render) {
db := GetDBInstance()
tip := models.Tips{}
api := models.API{}
total, _ := db.C("tips").Count()
index := getRandomIndex(total)
db.C("tips").Find(nil).Skip(index).One(&tip)
err := db.C("apis").Find(bson.M{"type": "json"}).One(&api)
api.Count++
if err != nil {
db.C("apis").Insert(&models.API{Id: bson.NewObjectId(), Type: "json", Count: 0})
} else {
db.C("apis").Update(bson.M{"type": "json"}, bson.M{"type": "json", "count": api.Count})
}
data, err := json.Marshal(api)
if err == nil {
sendAll(data)
}
if db.Session != nil {
defer db.Session.Close()
}
r.JSON(200, tip)
}
示例3: ArticlesIndex
func ArticlesIndex(r render.Render) {
hal := HAL{}
hal.Links = map[string]Link{
"self": Link{"/articles", false},
"show": Link{"/articles/{id}", true},
}
//result := []Todo{}
//
//rows, err := rethink.Table("todos").Run(session)
//if err != nil {
// log.Println(err)
//}
//
//for rows.Next() {
// var t Todo
// err := rows.Scan(&t)
// if err != nil {
// log.Println(err)
// }
// result = append(result, t)
//}
//r.JSON(200, result)
//r.JSON(200, "hello")
r.JSON(200, hal)
}
示例4: AddComment
// Called on a POST to /events/:id/comments
// Assuming valid comment; Adds the comment under the given id
func AddComment(db *mgo.Database, r render.Render, comment models.Comment, p martini.Params) {
var eventId bson.ObjectId
// Create a unique id
comment.Id = bson.NewObjectId()
if bson.IsObjectIdHex(p["event_id"]) {
eventId = bson.ObjectIdHex(p["event_id"])
} else {
r.JSON(400, "Bad Request: Invalid Event ID")
return
}
comment.EventId = eventId
// TODO Should be the user Id
comment.CreatedBy = bson.NewObjectId()
comment.CreatedAt = time.Now().UTC()
err := db.C("comments").Insert(comment)
if err != nil {
panic(err)
}
r.JSON(201, comment)
}
示例5: popularLinks
func popularLinks(r render.Render) {
links, err := store.Popular()
if err != nil {
log.WithField("package", "web").Error(err)
r.JSON(500, map[string]string{
"error": err.Error(),
})
return
}
var formattedLinks []map[string]interface{}
for _, link := range links {
formattedLinks = append(formattedLinks, map[string]interface{}{
"id": link.ID(),
"title": link.Title,
"url": link.URL,
"score": link.Score,
})
}
r.JSON(200, formattedLinks)
}
示例6: removeHero
func removeHero(db HeroDB, parms martini.Params, r render.Render) {
u := (db).(*heroDB)
id, _ := strconv.Atoi(parms["id"])
delete(u.heros, id)
r.JSON(http.StatusOK, nil)
}
示例7: GetPost
func GetPost(params martini.Params, r render.Render) {
hd := services.GetDb()
var results []models.Posts
hd.Where("id", "=", params["id"]).Limit(1).Find(&results)
r.JSON(200, results)
}
示例8: ListPosts
func ListPosts(params martini.Params, r render.Render) {
hd := services.GetDb()
var results []models.Posts
hd.Find(&results)
r.JSON(200, results)
}
示例9: ProjectGet
func ProjectGet(db gorm.DB, r render.Render, params martini.Params) {
var project models.Project
if err := db.First(&project, params["id"]).Error; err != nil {
r.JSON(http.StatusNotFound, map[string]interface{}{"error": "Project not found"})
return
}
r.JSON(http.StatusOK, project)
}
示例10: getDrop
func getDrop(params martini.Params, db *database.Database, r render.Render) {
d, err := db.GetDrop(params["url"])
if err != nil {
r.JSON(500, map[string]interface{}{"error": err.Error()})
} else {
r.JSON(200, map[string]interface{}{"drop": d})
}
}
示例11: deleteDrop
func deleteDrop(params martini.Params, db *database.Database, r render.Render) {
url := params["url"]
err := db.DeleteDrop(url)
if err != nil {
r.JSON(500, map[string]interface{}{"error": err.Error()})
} else {
r.JSON(200, map[string]interface{}{"ok": url})
}
}
示例12: renderTutorial
func renderTutorial(ren render.Render) {
var tutorial Tutorial
file, err := ioutil.ReadFile("neutrino.json")
if err != nil {
panic(err)
}
json.Unmarshal(file, &tutorial)
ren.JSON(200, tutorial)
}
示例13: allHeros
func allHeros(db HeroDB, parms martini.Params, r render.Render) {
u := (db).(*heroDB)
var heros []Hero
for _, v := range u.heros {
heros = append(heros, v)
}
r.JSON(http.StatusOK, heros)
}
示例14: listDrops
func listDrops(db *database.Database, r render.Render) {
// when there's none it returns a null?
drops, err := json.Marshal(db.ListDrops())
fmt.Println(drops)
if err != nil {
r.JSON(500, map[string]interface{}{"error": err})
} else {
r.JSON(200, map[string]interface{}{"drops": string(drops)})
}
}
示例15: HomeIndex
func HomeIndex(r render.Render) {
hal := HAL{}
hal.Links = map[string]Link{
"self": Link{"/", false},
"portfolio": Link{"/portfolio", false},
}
r.JSON(200, hal)
}