写惯了node服务端API,换到静态类型语言很不爽,规则一大堆~
要动态必需的!必须的!的!
这里注意几个包
json转换和解析用
github.com/bitly/go-simplejson
go get github.com/bitly/go-simplejson
body转换解析用 直接导入
strings io/ioutil
POSTMAN提交数据
{"data":"yes","hotel":["caonima","gelebi"],"HP":10000000}
控制器action 数据POST接收
package controllers import ( "github.com/revel/revel" "io/ioutil" "strings" console "fmt" simplejson"github.com/bitly/go-simplejson" ) type Hotels struct { *revel.Controller } type Dct map[string]interface {} func (c Hotels) Index() revel.Result { if strings.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 := range cn_hotel { console.Println("v",v) } } tt:=Dct{ "name":"春哥", "属性":Dct{"pet":"草尼马","Skill":[]string{"霸气春哥拳","原地复活"}}, } return c.RenderJson(tt) }
有疑问加站长微信联系(非本文作者)