求 JSON 2个函数

hellsam · · 806 次点击
```go package main import ( "encoding/json" "fmt" ) type Test struct { Name string Sex string Age int } type Test2 struct { Code int Msg Test } func JSON_Serialize(obj interface{}) (b []byte, err error) { b, err = json.Marshal(obj) if err != nil { return nil, err } return b, nil } func JSON_Deserialize(req []byte) (s map[string]interface{}, err error) { err = json.Unmarshal(req, &s) if err != nil { return nil, err } return s, nil } func main() { test := Test{"zy", "male", 20} test2 := Test2{0, test} req, _ := JSON_Serialize(&test2) fmt.Println(string(req)) req2map, _ := JSON_Deserialize([]byte(req)) fmt.Println(req2map) req2map["Code"] = 5 fmt.Println(req2map) } ```
#11
更多评论
你这不是已经写了吗?
#1
<a href="/user/channel" title="@channel">@channel</a> 这个代码有问题 所以来请教各位大牛
#2