{"id":125006,"date":"2018-10-30T10:46:21","date_gmt":"2018-10-30T02:46:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=125006"},"modified":"2018-10-22T10:41:07","modified_gmt":"2018-10-22T02:41:07","slug":"golang-json-sovle","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/golang-json-sovle.html","title":{"rendered":"\u4ecb\u7ecd golang json\u6570\u636e\u7684\u5904\u7406"},"content":{"rendered":"

\u4f7f\u7528golang\u4e0b\u7684net\/http\u6a21\u5757\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5b9e\u73b0webserver\u529f\u80fd\u3002\u672c\u7bc7\u5c31\u7ed3\u5408http\u6a21\u5757\u5728POST\u53d1\u9001josn\u6570\u636e\u7ed9webserver\u4ee5\u53cawebserver\u5728\u6536\u5230json\u6570\u636e\u540e\u5982\u4f55\u5904\u7406\u3002<\/p>\n

\u4e00\u3001server\u7aef\u5904\u7406json\u6570\u636e<\/strong><\/div>\n

server\u7aef\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

package main\r\nimport (\r\n\"net\/http\"\r\n\"fmt\"\r\n\"log\"\r\n\"encoding\/json\"\r\n)\r\ntype User struct{\r\nId string\r\nBalance uint64\r\n}\r\nfunc main() {\r\nhttp.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\r\nvar u User\r\nif r.Body == nil {\r\nhttp.Error(w, \"Please send a request body\", 400)\r\nreturn\r\n}\r\nerr := json.NewDecoder(r.Body).Decode(&u)\r\nif err != nil {\r\nhttp.Error(w, err.Error(), 400)\r\nreturn\r\n}\r\nfmt.Println(u.Id)\r\n})\r\nlog.Fatal(http.ListenAndServe(\":8080\", nil))\r\n}\r\n<\/pre>\n

\u901a\u8fc7go run server.go\u8fd0\u884c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7curl \u547d\u4ee4\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n

curl http:\/\/127.0.0.1:8080 -d '{\"Id\": \"www.361way.com\", \"Balance\": 8}'\r\n<\/pre>\n

\u901a\u8fc7curl\u547d\u4ee4\u6267\u884c\u540e\uff0c\u5728server\u7aef\u5c4f\u5e55\u4e0a\u80fd\u6b63\u5e38\u6253\u5370www.361way.com\u5c31\u8868\u793aserver\u7aef\u5df2\u6b63\u5e38\u5904\u7406json\u6570\u636e\u3002<\/p>\n

\u4e8c\u3001client\u7aef\u7684json post\u5904\u7406<\/strong><\/div>\n

client\u7aef\u5b9e\u73b0\u7684\u529f\u80fd\u5c31\u662f\u4e0a\u9762curl\u547d\u4ee4\u6267\u884c\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

package main\r\nimport (\r\n\"net\/http\"\r\n\"encoding\/json\"\r\n\"io\"\r\n\"os\"\r\n\"bytes\"\r\n)\r\ntype User struct{\r\nId string\r\nBalance uint64\r\n}\r\nfunc main() {\r\nu := User{Id: \"www.361way.com\", Balance: 8}\r\nb := new(bytes.Buffer)\r\njson.NewEncoder(b).Encode(u)\r\nres, _ := http.Post(\"http:\/\/127.0.0.1:8080\", \"application\/json; charset=utf-8\", b)\r\nio.Copy(os.Stdout, res.Body)\r\n}\r\n<\/pre>\n
\u4e09\u3001\u670d\u52a1\u7aef\u8fd4\u56dejson\u6570\u636e<\/strong><\/div>\n

\u907f\u514d\u53ef\u80fd\u63cf\u8ff0\u7684\u6b67\u4e49\uff0c\u8fd9\u91cc\u7528\u82f1\u6587\u63cf\u8ff0\u4e3a\u201cEncoding JSON in a server response\u201d\uff0c\u5373\u901a\u8fc7\u670d\u52a1\u5668\u5904\u7406\u540e\uff0c\u5c06json\u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<\/p>\n

package main\r\nimport (\r\n\"net\/http\"\r\n\"log\"\r\n\"encoding\/json\"\r\n)\r\ntype User struct{\r\nId string\r\nBalance uint64\r\n}\r\nfunc main() {\r\nhttp.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\r\nu := User{Id: \"www.361way.com\", Balance: 8}\r\njson.NewEncoder(w).Encode(u)\r\n})\r\nlog.Fatal(http.ListenAndServe(\":8080\", nil))\r\n}\r\n<\/pre>\n

\u901a\u8fc7curl http:\/\/127.0.0.1:8080 \u5176\u4f1a\u8fd4\u56de{Id: \"www.361way.com\", Balance: 8}\u6570\u636e\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n

\u56db\u3001\u8fd4\u56de\u54cd\u5e94\u5934\u4fe1\u606f<\/strong><\/div>\n

\u5728\u5411\u670d\u52a1\u7aef\u53d1\u9001\u6570\u636e\u540e\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u83b7\u53d6\u54cd\u5e94\u5934\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u5904\u7406\uff1a<\/p>\n

package main\r\nimport (\r\n\"net\/http\"\r\n\"encoding\/json\"\r\n\"bytes\"\r\n\"fmt\"\r\n)\r\ntype User struct{\r\nId string\r\nBalance uint64\r\n}\r\nfunc main() {\r\nu := User{Id: \"www.361way.com\", Balance: 8}\r\nb := new(bytes.Buffer)\r\njson.NewEncoder(b).Encode(u)\r\nres, _ := http.Post(\"https:\/\/httpbin.org\/post\", \"application\/json; charset=utf-8\", b)\r\nvar body struct {\r\n\/\/sends back key\/value pairs, no map[string][]string\r\nHeaders map[string]string `json:\"headers\"`\r\nOrigin string `json:\"origin\"`\r\n}\r\njson.NewDecoder(res.Body).Decode(&body)\r\nfmt.Println(body)\r\n}\r\n<\/pre>\n

\u4ee5\u4e0a\u4ee3\u7801\u5728\u5411httpbin.org\u00a0 post\u6570\u636e\u540e\uff0c\u4f1a\u5f97\u5230\u5982\u4e0b\u54cd\u5e94\u4fe1\u606f\uff1a<\/p>\n

{map[Content-Length:36 Content-Type:application\/json; charset=utf-8 Host:httpbin.org User-Agent:Go-http-client\/1.1 Accept-Encoding:gzip Connection:close] 115.28.174.118}\r\n<\/pre>\n
<\/div>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528golang\u4e0b\u7684net\/http\u6a21\u5757\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5b9e\u73b0webserver\u529f\u80fd\u3002\u672c\u7bc7\u5c31\u7ed3\u5408http\u6a21\u5757\u5728PO […]<\/p>\n","protected":false},"author":643,"featured_media":125017,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-125006","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=125006"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125006\/revisions"}],"predecessor-version":[{"id":125078,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/125006\/revisions\/125078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/125017"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=125006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=125006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=125006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}