當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Render.JSON方法代碼示例

本文整理匯總了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})
}
開發者ID:AndrewJHart,項目名稱:irlmoji,代碼行數:27,代碼來源:handlers_irlmoji.go

示例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)
}
開發者ID:lihex,項目名稱:vim-tips-web,代碼行數:30,代碼來源:api.go

示例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)

}
開發者ID:runemadsen,項目名稱:api.runemadsen.com,代碼行數:31,代碼來源:articles_controller.go

示例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)
}
開發者ID:rolandjudd,項目名稱:thingstodo,代碼行數:28,代碼來源:comment.go

示例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)
}
開發者ID:mcmillan,項目名稱:socialite,代碼行數:26,代碼來源:web.go

示例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)
}
開發者ID:pombredanne,項目名稱:trifles,代碼行數:8,代碼來源:main.go

示例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)
}
開發者ID:judg3,項目名稱:blog,代碼行數:8,代碼來源:posts.go

示例8: ListPosts

func ListPosts(params martini.Params, r render.Render) {
	hd := services.GetDb()

	var results []models.Posts
	hd.Find(&results)

	r.JSON(200, results)
}
開發者ID:judg3,項目名稱:blog,代碼行數:8,代碼來源:posts.go

示例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)
}
開發者ID:jerryclinesmith,項目名稱:notabbble,代碼行數:8,代碼來源:project_controller.go

示例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})
	}
}
開發者ID:ZachOrr,項目名稱:dropzor,代碼行數:8,代碼來源:server.go

示例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})
	}
}
開發者ID:ZachOrr,項目名稱:dropzor,代碼行數:9,代碼來源:server.go

示例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)
}
開發者ID:jpatel531,項目名稱:neutrino-server,代碼行數:9,代碼來源:main.go

示例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)
}
開發者ID:pombredanne,項目名稱:trifles,代碼行數:9,代碼來源:main.go

示例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)})
	}
}
開發者ID:ZachOrr,項目名稱:dropzor,代碼行數:10,代碼來源:server.go

示例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)
}
開發者ID:runemadsen,項目名稱:api.runemadsen.com,代碼行數:11,代碼來源:home_controller.go


注:本文中的github.com/codegangsta/martini-contrib/render.Render.JSON方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。