写惯了node服务端API,换到静态类型语言很不爽,规则一大堆~
要动态必需的!必须的!的!
这里注意几个包
json转换和解析用
github.com/bitly/go-simplejson
gogetgithub.com/bitly/go-simplejson
body转换解析用 直接导入
strings io/IoUtil
POSTMAN提交数据
{"data":"yes","hotel":["caonima","gelebi"],"HP":10000000}
控制器action 数据POST接收
packagecontrollers import( "github.com/revel/revel" "io/IoUtil" "strings" console"fmt" simplejson"github.com/bitly/go-simplejson" ) typeHotelsstruct{ *revel.Controller } typeDctmap[string]interface{} func(cHotels)Index()revel.Result{ ifstrings.Contains(c.Request.ContentType,"application/json"){ content,_:=IoUtil.ReadAll(c.Request.Body) cn_json,_:=simplejson.NewJson([]byte(content)) console.Println(cn_json) cn_body,_:=cn_json.Get("data").String() cn_hotel,_:=cn_json.Get("hotel").Array() cn_hp,_:=cn_json.Get("HP").Int() console.Println(cn_hp) console.Println(cn_body) for_,v:=rangecn_hotel{ console.Println("v",v) } } tt:=Dct{ "name":"春哥","属性":Dct{"pet":"草尼马","Skill":[]string{"霸气春哥拳","原地复活"}},} returnc.RenderJson(tt) }