LoginSignup
0
0

More than 5 years have passed since last update.

Go言語を真剣に勉強してみた〜RevelでJSONを扱って見よう〜

Posted at

はじめに

こんにちは。某学校でプログラミング等の勉強中のサーバーサイドのプログラマーのワタタクです。:relaxed:
今回はRevelを使っての、JSONについて書いていきます。

Revelを用いるとJSONも簡単に扱うことができるので、早速やっていきましょ。

RevelでのJSONの扱い

  • 1. app/controllers/json.goを作る
json.go
package controllers

type JsonResponce struct {
    Responce interface{} `json:"responce"`
} 
  • 2.app/controllers/~~~~_api.goを作る
~~~~_api.go
package controllers

import (
    "github.com/revel/revel"
)

type Api struct {
    *revel.Controller
}

func (c Api) GetApi() revel.Result {

    //何らかの処理を記述(DB処理など)

    response := JsonResponce{}
    response.Responce = "レスポンス" //ここで返すレスポンスを指定

    return c.RenderJSON(response)
}

TIPS

JSONで飛んできた値を受け取る

 c.Params.Get("キー")

以上。短いですがこれでJSONの処理は何とかなります。
あとは煮るなり焼くなりして下さい。
もし何か間違っている等のご指摘があればご連絡ください。
最後まで読んで頂きありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0