golang中使用json的一些例子
// go_json_test project main.gopackage mainimport ( "encoding/json" "fmt")func encode_from_map_test() { fmt.Printf("encode_from_map_test\n") m := map[string][]string{ "level": {"debug"}, "message": {"File not found", "Stack overflow"}, } if data, err := json.Marshal(m); err == nil { fmt.Printf("%s\n", data) }}type GameInfo struct { Game_id int64 Game_map string Game_time int32}func encode_from_object_test() { fmt.Printf("encode_from_object_test\n") game_infos := []GameInfo{ GameInfo{1, "map1", 20}, GameInfo{2, "map2", 60}, } if data, err := json.Marshal(game_infos); err == nil { fmt.Printf("%s\n", data) }}type GameInfo1 struct { Game_id int64 `json:"game_id"` // Game_id解析為game_id Game_map string `json:"game_map,omitempty"` // GameMap解析為game_map, 忽略空置 Game_time int32 Game_name string `json:"-"` // 忽略game_name}func encode_from_object_tag_test() { fmt.Printf("encode_from_object_tag_test\n") game_infos := []GameInfo1{ GameInfo1{1, "map1", 20, "name1"}, GameInfo1{2, "map2", 60, "name2"}, GameInfo1{3, "", 120, "name3"}, } if data, err := json.Marshal(game_infos); err == nil { fmt.Printf("%s\n", data) }}type BaseObject struct { Field_a string Field_b string}type DeriveObject struct { BaseObject Field_c string}func encode_from_object_with_anonymous_field() { fmt.Printf("encode_from_object_with_anonymous_field\n") object := DeriveObject{BaseObject{"a", "b"}, "c"} if data, err := json.Marshal(object); err == nil { fmt.Printf("%s\n", data) }}// convert interface// 在調用Marshal(v interface{})時,該函數會判斷v是否滿足json.Marshaler或者 encoding.Marshaler 介面,// 如果滿足,則會調用這兩個介面來進行轉換(如果兩個都滿足,優先調用json.Marshaler)/*// json.Marshalertype Marshaler interface { MarshalJSON() ([]byte, error)}// encoding.TextMarshalertype TextMarshaler interface { MarshalText() (text []byte, err error)}*/type Point struct { X int Y int}func (pt Point) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`{"px" : %d, "py" : %d}`, pt.X, pt.Y)), nil}func encode_from_object_with_marshaler_interface() { fmt.Printf("encode_from_object_with_marshaler_interface\n") if data, err := json.Marshal(Point{50, 50}); err == nil { fmt.Printf("%s\n", data) } else { fmt.Printf("error %s\n", err.Error()) }}type Point1 struct { X int Y int}func (pt Point1) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf(`{"px" : %d, "py" : %d}`, pt.X, pt.Y)), nil}func encode_from_object_with_text_marshaler_interface() { fmt.Printf("encode_from_object_with_text_marshaler_interface\n") if data, err := json.Marshal(Point1{50, 50}); err == nil { fmt.Printf("%s\n", data) } else { fmt.Printf("error %s\n", err.Error()) }}// decode testfunc decode_to_map_test() { fmt.Printf("decode_to_map_test\n") data := `[{"Level":"debug","Msg":"File: \"test.txt\" Not Found"},` + `{"Level":"","Msg":"Logic error"}]` var debug_infos []map[string]string json.Unmarshal([]byte(data), &debug_infos) fmt.Println(debug_infos)}type DebugInfo struct { Level string Msg string author string // 未匯出欄位不會被json解析}func (dbgInfo DebugInfo) String() string { return fmt.Sprintf("{Level: %s, Msg: %s}", dbgInfo.Level, dbgInfo.Msg)}func decode_to_object_test() { fmt.Printf("decode_to_object_test\n") data := `[{"level":"debug","msg":"File Not Found","author":"Cynhard"},` + `{"level":"","msg":"Logic error","author":"Gopher"}]` var dbgInfos []DebugInfo json.Unmarshal([]byte(data), &dbgInfos) fmt.Println(dbgInfos)}type DebugInfoTag struct { Level string `json:"level"` // level 解碼為 Level Msg string `json:"message"` // message 解碼為 Msg Author string `json:"-"` // 忽略Author}func (dbgInfo DebugInfoTag) String() string { return fmt.Sprintf("{Level: %s, Msg: %s}", dbgInfo.Level, dbgInfo.Msg)}func decode_to_object_tag_test() { fmt.Printf("decode_to_object_tag_test\n") data := `[{"level":"debug","message":"File Not Found","author":"Cynhard"},` + `{"level":"","message":"Logic error","author":"Gopher"}]` var dbgInfos []DebugInfoTag json.Unmarshal([]byte(data), &dbgInfos) fmt.Println(dbgInfos)}type Pointx struct{ X, Y int }type Circle struct { Pointx Radius int}func (c Circle) String() string { return fmt.Sprintf("{Point{X: %d, Y :%d}, Radius: %d}", c.Pointx.X, c.Pointx.Y, c.Radius)}func decode_to_object_with_anonymous_field() { fmt.Printf("decode_to_object_with_anonymous_field\n") data := `{"X":80,"Y":80,"Radius":40}` var c Circle json.Unmarshal([]byte(data), &c) fmt.Println(c)}// decode convert interface// 解碼時根據參數是否滿足json.Unmarshaler和encoding.TextUnmarshaler來調用相應函數(若兩個函數都存在,則優先調用UnmarshalJSON)/*// json.Unmarshalertype Unmarshaler interface { UnmarshalJSON([]byte) error}// encoding.TextUnmarshalertype TextUnmarshaler interface { UnmarshalText(text []byte) error}*/type Pointy struct{ X, Y int }func (pt Pointy) MarshalJSON() ([]byte, error) { // no decode, just print return []byte(fmt.Sprintf(`{"X":%d,"Y":%d}`, pt.X, pt.Y)), nil}func decode_to_object_with_marshaler_interface() { fmt.Printf("decode_to_object_with_marshaler_interface\n") if data, err := json.Marshal(Pointy{50, 50}); err == nil { fmt.Printf("%s\n", data) }}func main() { fmt.Println("json test!") fmt.Printf("ecode test\n") encode_from_map_test() encode_from_object_test() encode_from_object_tag_test() encode_from_object_with_anonymous_field() encode_from_object_with_marshaler_interface() encode_from_object_with_text_marshaler_interface() fmt.Printf("decode test\n") decode_to_map_test() decode_to_object_test() decode_to_object_tag_test() decode_to_object_with_anonymous_field() decode_to_object_with_marshaler_interface()}
運行結果:
json test!ecode testencode_from_map_test{"level":["debug"],"message":["File not found","Stack overflow"]}encode_from_object_test[{"Game_id":1,"Game_map":"map1","Game_time":20},{"Game_id":2,"Game_map":"map2","Game_time":60}]encode_from_object_tag_test[{"game_id":1,"game_map":"map1","Game_time":20},{"game_id":2,"game_map":"map2","Game_time":60},{"game_id":3,"Game_time":120}]encode_from_object_with_anonymous_field{"Field_a":"a","Field_b":"b","Field_c":"c"}encode_from_object_with_marshaler_interface{"px":50,"py":50}encode_from_object_with_text_marshaler_interface"{\"px\" : 50, \"py\" : 50}"decode testdecode_to_map_test[map[Level:debug Msg:File: "test.txt" Not Found] map[Level: Msg:Logic error]]decode_to_object_test[{Level: debug, Msg: File Not Found} {Level: , Msg: Logic error}]decode_to_object_tag_test[{Level: debug, Msg: File Not Found} {Level: , Msg: Logic error}]decode_to_object_with_anonymous_field{Point{X: 80, Y :80}, Radius: 40}decode_to_object_with_marshaler_interface{"X":50,"Y":50}