{"id":57668,"date":"2024-10-10T20:08:15","date_gmt":"2024-10-10T12:08:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57668\/"},"modified":"2024-10-10T20:08:15","modified_gmt":"2024-10-10T12:08:15","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-json-%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57668\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00 JSON \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80-json-%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c-roOIvOJJBj\">GO\u6559\u7a0b: Go \u8a9e\u8a00 JSON \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0cJSON\uff08JavaScript Object Notation\uff09\u5df2\u6210\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\u3002Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6a19\u6e96\u5eab\u4f86\u8655\u7406 JSON \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c JSON \u6587\u4ef6\u7684\u8b80\u53d6\u548c\u5beb\u5165\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u7bc4\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"json-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-roOIvOJJBj\">JSON \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>JSON \u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u4ea4\u63db\u683c\u5f0f\uff0c\u6613\u65bc\u4eba\u985e\u95b1\u8b80\u548c\u7de8\u5beb\uff0c\u540c\u6642\u4e5f\u6613\u65bc\u6a5f\u5668\u89e3\u6790\u548c\u751f\u6210\u3002\u5b83\u7684\u57fa\u672c\u7d50\u69cb\u7531\u9375\u503c\u5c0d\u7d44\u6210\uff0c\u901a\u5e38\u7528\u65bc\u5b58\u5132\u548c\u50b3\u8f38\u7d50\u69cb\u5316\u6578\u64da\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84-json-%e8%99%95%e7%90%86-roOIvOJJBj\">Go \u8a9e\u8a00\u4e2d\u7684 JSON \u8655\u7406<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u4e2d\u5305\u542b\u4e86 <code>encoding\/json<\/code> \u5305\uff0c\u9019\u500b\u5305\u63d0\u4f9b\u4e86\u7de8\u78bc\u548c\u89e3\u78bc JSON \u7684\u529f\u80fd\u3002\u4f7f\u7528\u9019\u500b\u5305\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07 Go \u7684\u6578\u64da\u7d50\u69cb\u8f49\u63db\u70ba JSON \u683c\u5f0f\uff0c\u6216\u5c07 JSON \u683c\u5f0f\u7684\u6578\u64da\u89e3\u6790\u70ba Go \u7684\u6578\u64da\u7d50\u69cb\u3002<\/p>\n<h3 id=\"%e8%ae%80%e5%8f%96-json-%e6%96%87%e4%bb%b6-roOIvOJJBj\">\u8b80\u53d6 JSON \u6587\u4ef6<\/h3>\n<p>\u8981\u5f9e JSON \u6587\u4ef6\u4e2d\u8b80\u53d6\u6578\u64da\uff0c\u6211\u5011\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u8207 JSON \u7d50\u69cb\u76f8\u5c0d\u61c9\u7684 Go \u7d50\u69cb\u9ad4\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"io\/ioutil\"\n    \"log\"\n)\n\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc main() {\n    \/\/ \u8b80\u53d6 JSON \u6587\u4ef6\n    data, err := ioutil.ReadFile(\"person.json\")\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    \/\/ \u89e3\u6790 JSON \u6578\u64da\n    var person Person\n    if err := json.Unmarshal(data, &amp;person); err != nil {\n        log.Fatal(err)\n    }\n\n    fmt.Printf(\"Name: %s, Age: %dn\", person.Name, person.Age)\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b <code>Person<\/code> \u7d50\u69cb\u9ad4\uff0c\u4e26\u4f7f\u7528 <code>ioutil.ReadFile<\/code> \u51fd\u6578\u8b80\u53d6 JSON \u6587\u4ef6\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528 <code>json.Unmarshal<\/code> \u51fd\u6578\u5c07 JSON \u6578\u64da\u89e3\u6790\u5230 <code>Person<\/code> \u7d50\u69cb\u9ad4\u4e2d\u3002<\/p>\n<h3 id=\"%e5%af%ab%e5%85%a5-json-%e6%96%87%e4%bb%b6-roOIvOJJBj\">\u5beb\u5165 JSON \u6587\u4ef6<\/h3>\n<p>\u9664\u4e86\u8b80\u53d6 JSON \u6587\u4ef6\uff0cGo \u8a9e\u8a00\u4e5f\u652f\u6301\u5c07\u6578\u64da\u5beb\u5165 JSON \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5c07 Go \u7d50\u69cb\u9ad4\u8f49\u63db\u70ba JSON \u4e26\u5beb\u5165\u6587\u4ef6\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"os\"\n)\n\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc main() {\n    person := Person{Name: \"Alice\", Age: 30}\n\n    \/\/ \u5c07\u7d50\u69cb\u9ad4\u8f49\u63db\u70ba JSON\n    data, err := json.Marshal(person)\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n\n    \/\/ \u5beb\u5165 JSON \u6587\u4ef6\n    if err := ioutil.WriteFile(\"person.json\", data, 0644); err != nil {\n        fmt.Println(err)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b <code>Person<\/code> \u7d50\u69cb\u9ad4\u7684\u5be6\u4f8b\uff0c\u7136\u5f8c\u4f7f\u7528 <code>json.Marshal<\/code> \u5c07\u5176\u8f49\u63db\u70ba JSON \u683c\u5f0f\u7684\u6578\u64da\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code>ioutil.WriteFile<\/code> \u5c07 JSON \u6578\u64da\u5beb\u5165\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-roOIvOJJBj\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406 JSON \u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\u3002\u901a\u904e\u4f7f\u7528 <code>encoding\/json<\/code> \u5305\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07 Go \u7684\u6578\u64da\u7d50\u69cb\u8f49\u63db\u70ba JSON \u683c\u5f0f\uff0c\u6216\u5c07 JSON \u6578\u64da\u89e3\u6790\u70ba Go \u7684\u6578\u64da\u7d50\u69cb\u3002\u9019\u4f7f\u5f97 Go \u8a9e\u8a00\u5728\u8655\u7406\u6578\u64da\u4ea4\u63db\u548c\u5b58\u5132\u65b9\u9762\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 Go \u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u8b80\u53d6\u548c\u5beb\u5165 JSON \u6587\u4ef6\uff0c\u638c\u63e1\u57fa\u672c\u64cd\u4f5c\u548c\u5be6\u7528\u7bc4\u4f8b\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57668","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57668"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57668\/revisions"}],"predecessor-version":[{"id":57669,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57668\/revisions\/57669"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}