@@ -70,3 +70,83 @@ func MD5_LollipopGO(data string) string {
7070}
7171
7272//------------------------------------------------------------------------------
73+
74+ // package main
75+
76+ // import (
77+ // "encoding/json"
78+ // "fmt"
79+ // "os"
80+ // )
81+
82+ // type ConfigStruct struct {
83+ // Host string `json:"host"`
84+ // Port int `json:"port"`
85+ // AnalyticsFile string `json:"analytics_file"`
86+ // StaticFileVersion int `json:"static_file_version"`
87+ // StaticDir string `json:"static_dir"`
88+ // TemplatesDir string `json:"templates_dir"`
89+ // SerTcpSocketHost string `json:"serTcpSocketHost"`
90+ // SerTcpSocketPort int `json:"serTcpSocketPort"`
91+ // Fruits []string `json:"fruits"`
92+ // }
93+
94+ // type Other struct {
95+ // SerTcpSocketHost string `json:"serTcpSocketHost"`
96+ // SerTcpSocketPort int `json:"serTcpSocketPort"`
97+ // Fruits []string `json:"fruits"`
98+ // }
99+
100+ // func main() {
101+ // jsonStr := `{"host": "http://localhost:9090","port": 9090,"analytics_file": "","static_file_version": 1,"static_dir": "E:/Project/goTest/src/","templates_dir": "E:/Project/goTest/src/templates/","serTcpSocketHost": ":12340","serTcpSocketPort": 12340,"fruits": ["apple", "peach"]}`
102+
103+ // //json str 转map
104+ // var dat map[string]interface{}
105+ // if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
106+ // fmt.Println("==============json str 转map=======================")
107+ // fmt.Println(dat)
108+ // fmt.Println(dat["host"])
109+ // }
110+
111+ // //json str 转struct
112+ // var config ConfigStruct
113+ // if err := json.Unmarshal([]byte(jsonStr), &config); err == nil {
114+ // fmt.Println("================json str 转struct==")
115+ // fmt.Println(config)
116+ // fmt.Println(config.Host)
117+ // }
118+
119+ // //json str 转struct(部份字段)
120+ // var part Other
121+ // if err := json.Unmarshal([]byte(jsonStr), &part); err == nil {
122+ // fmt.Println("================json str 转struct==")
123+ // fmt.Println(part)
124+ // fmt.Println(part.SerTcpSocketPort)
125+ // }
126+
127+ // //struct 到json str
128+ // if b, err := json.Marshal(config); err == nil {
129+ // fmt.Println("================struct 到json str==")
130+ // fmt.Println(string(b))
131+ // }
132+
133+ // //map 到json str
134+ // fmt.Println("================map 到json str=====================")
135+ // enc := json.NewEncoder(os.Stdout)
136+ // enc.Encode(dat)
137+
138+ // //array 到 json str
139+ // arr := []string{"hello", "apple", "python", "golang", "base", "peach", "pear"}
140+ // lang, err := json.Marshal(arr)
141+ // if err == nil {
142+ // fmt.Println("================array 到 json str==")
143+ // fmt.Println(string(lang))
144+ // }
145+
146+ // //json 到 []string
147+ // var wo []string
148+ // if err := json.Unmarshal(lang, &wo); err == nil {
149+ // fmt.Println("================json 到 []string==")
150+ // fmt.Println(wo)
151+ // }
152+ // }
0 commit comments