diff --git a/.example/encoding/gjson/issue283.go b/.example/encoding/gjson/issue283.go new file mode 100644 index 000000000..7e01a7926 --- /dev/null +++ b/.example/encoding/gjson/issue283.go @@ -0,0 +1,69 @@ +package main + +import ( + "github.com/gogf/gf/encoding/gjson" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/os/glog" +) + +type GameUser struct { + Uid int `json:"uid"` + Account string `json:"account"` + Tel string `json:"tel"` + Role string `json:"role"` + Vip int `json:"vip"` + GameLevel int `json:"gamelevel"` + Diamond int `json:"diamond"` + Coin int `json:"coin"` + Value int `json:"value"` + Area string `json:"area"` + ServerName string `json:"servername"` + Time int `json:"time"` + ClientInfo *ClientInfo `json:"client_info"` +} + +type ClientInfo struct { + ClientGuid string `json:"client_guid"` + ClientType int `json:"client_type"` + ClientSDKVersion string `json:"client_sdk_version"` + ClientVersion string `json:"client_version"` + PackageId string `json:"packageid"` + PhoneType string `json:"phone_type"` + DevicesId string `json:"devices_id"` + ClientMac string `json:"client_mac"` +} + +func main() { + s := `{ + "uid":9527, + "account":"zhangsan", + "tel":"15248787", + "role":"test", + "vip":7, + "gamelevel":59, + "diamond ":59, + "coin ":59, + "value ":99, + "area":"s", + "servername":"灵动", + "time":15454878787, + "client_info": { + "client_guid": "aaaa", + "client_type": 1, + "client_sdk_version": "1.0.1", + "client_version": "1.0.1", + "packageid":"", + "phone_type": "vivi", + "devices_id":"", + "client_mac":"" + } +}` + + gameUser := &GameUser{} + err := gjson.DecodeTo(s, gameUser) + if err != nil { + glog.Error(err) + } + g.Dump(gameUser) + +} diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 123b2737d..b91f60db7 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -106,15 +106,7 @@ func (r *Request) GetRawString() string { // 获取原始json请求输入字符串,并解析为json对象 func (r *Request) GetJson() *gjson.Json { - data := r.GetRaw() - if len(data) > 0 { - if j, err := gjson.DecodeToJson(data); err == nil { - return j - } else { - r.Error(err, ": ", string(data)) - } - } - return nil + return gjson.New(r.GetRaw()) } func (r *Request) GetString(key string, def ...interface{}) string {