{"id":489,"date":"2020-03-31T20:34:23","date_gmt":"2020-03-31T12:34:23","guid":{"rendered":"http:\/\/124.244.86.40:8080\/wordpress\/?p=489"},"modified":"2020-03-31T20:34:23","modified_gmt":"2020-03-31T12:34:23","slug":"golang-struct-%e8%88%87-json-%e7%9a%84%e5%bf%ab%e9%80%9f%e8%bd%89%e6%8f%9b%e6%b3%95","status":"publish","type":"post","link":"https:\/\/imuslab.com\/wordpress\/2020\/03\/31\/golang-struct-%e8%88%87-json-%e7%9a%84%e5%bf%ab%e9%80%9f%e8%bd%89%e6%8f%9b%e6%b3%95\/","title":{"rendered":"Golang Struct \u8207 JSON \u7684\u5feb\u901f\u8f49\u63db\u6cd5"},"content":{"rendered":"\n<p>\u4f5c\u70ba\u524d PHP \u958b\u767c\u8005\uff0c\u4e00\u8aaa\u5230 Golang \u8981\u628a\u6771\u897f\u8f49\u505a JSON \u5c31\u982d\u75db\uff0c\u56e0\u70ba\u5728 php \u88e1\u9762\u4f60\u53ef\u4ee5\u975e\u5e38\u7c21\u55ae\u7684\u628a\u4e00\u500b\u8907\u96dc\u7684\u7d50\u69cb\u8f49\u63db\u6210 json\uff0c\u4f8b\u5982\u8aaa\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"170\" src=\"http:\/\/124.244.86.40:8080\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-32-38.png\" alt=\"\" class=\"wp-image-492\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-32-38.png 707w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-32-38-300x72.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p>\u5c31\u80fd\u5920\u8f38\u51fa<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"101\" src=\"http:\/\/124.244.86.40:8080\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-33-25.png\" alt=\"\" class=\"wp-image-494\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-33-25.png 584w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2020\/03\/2020-03-31_20-33-25-300x52.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<p>\u6240\u4ee5\u5728\u958b\u767c php \u7684\u6642\u5019\u57fa\u672c\u4e0a\u662f\u4e0d\u7528\u7279\u5225\u8655\u7406 json \u7684\u8f49\u63db\u7684\uff0c\u5230\u4e86 javascript \u4e00\u7aef\u4e5f\u80fd\u5920\u8f15\u6613\u7684\u628a\u6536\u56de\u4f86\u7684\u8cc7\u6599\u76f4\u63a5\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.get(\"url_here\",function(data){\n    console.log(data&#91;0]);\n});\n\/\/ \u8f38\u51fa \"Hello\"<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u53bb\u5230 Golang \u4e4b\u5f8c\u5beb\u6cd5\u5c31\u5b8c\u5168\u4e0d\u540c\u4e86\uff0c\u5728\u9019\u7bc7\u7c21\u77ed\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u6703\u7c21\u55ae\u4ecb\u7d39\u4e00\u4e0b\u600e\u6a23\u8f15\u6613\u7684\u628a\u4efb\u4f55\u8cc7\u6599 parse \u6210 JSON string \u53ca\u7531 JSON string \u8f49\u56de\u539f\u672c\u578b\u614b\u7684\u65b9\u6cd5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Golang \u7684 Struct \u662f\u4e00\u6a23\u5f88\u795e\u5947\u7684\u6771\u897f<\/h2>\n\n\n\n<p>\u751a\u9ebc\u662f Golang Struct \u5462\uff1f\u9019\u500b\u8ddf C \u8a9e\u8a00\u88e1\u7684\u6c92\u5dee\u592a\u9060\uff0c\u7c21\u55ae\u4f86\u8aaa\u5c31\u662f Golang \u4e0a OOP \u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Struct \u4f8b\u5b50\uff08\u539f\u78bc\u53d6\u81ea ArOZ Online 1.0\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type desktopObject struct{\n\tFilepath string;\n\tFilename string;\n\tExt string;\n\tIsDir bool;\n\tIsShortcut bool;\n\tIconX int;\n\tIconY int;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u628a Struct \u8f49\u63db\u6210 JSON<\/h2>\n\n\n\n<p>\u9019\u662f\u4e00\u500b\u7528\u4f86\u5b9a\u7fa9\u684c\u9762\u4e0a\u7684\u7269\u54c1\u7684 struct\u3002\u8981\u628a\u5b83\u8f49\u5230 JSON \u5341\u5206\u7c21\u55ae\uff0c\u9996\u5148\u4f60\u8981\u65b0\u5efa\u4e00\u500b Object<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisFileObject := new(desktopObject)<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u628a\u8cc7\u6599\u586b\u9032\u53bb\uff0c\u4f8b\u5982\u9019\u6a23<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisFileObject.Filepath = path;\nthisFileObject.Filename = filepath.Base(path)\nthisFileObject.Ext = filepath.Ext(path)\nthisFileObject.IsDir = IsDir(path)<\/code><\/pre>\n\n\n\n<p>\u6700\u5f8c\u628a\u5b83\u58d3\u6210\u4e00\u500b JSON String<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jsonString, err:= json.Marshal(desktopFiles); \nif (err != nil){\n    \/\/\u932f\u8aa4\u8655\u7406\n}\nreturn string(jsonString);<\/code><\/pre>\n\n\n\n<p>\u5c31\u662f\u9019\u6a23\u4f60\u7684 JSON String \u5c31\u505a\u597d\u4e86<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7531 JSON \u8f49\u56de Struct<\/h2>\n\n\n\n<p>\u9019\u500b\u5c31\u96e3\u4e00\u9ede\u4e86\uff0c\u4f46\u662f\u9084\u662f\u80fd\u505a\u5230\u7684\u3002\u7c21\u55ae\u4f86\u8aaa\u5c31\u662f\u628a JSON string map \u5230\u4e00\u500b\u73fe\u6709\u7684 struct \u4e0a\u9762\u53bb\u3002\u5148\u5047\u8a2d\u6211\u5011\u6709\u4ee5\u4e0b\u7684\u4e00\u500b struct<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type iconLocation struct{\n\tX int;\n\tY int;\n}<\/code><\/pre>\n\n\n\n<p>\u548c\u4e00\u500b JSON string<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jsonstring := \"\u4f60\u8981\u8f49\u63db\u7684\u6771\u897f\"<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u4f60\u8ddf\u4e0a\u9762\u505a\u7684\u4e00\u6a23\uff0c\u5148\u9810\u7559\u4e00\u500b\u8b8a\u6578\u7d66 json unmarshal \u6642\u7528\u3002\u7136\u5f8c\u628a json string \u8f49\u63db\u5230\u9019\u500b struct \u88e1\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisLocation := new(iconLocation)\njson.Unmarshal(&#91;]byte(jsonstring ), &amp;thisLocation)\nfmt.Println(thisLocation)<\/code><\/pre>\n\n\n\n<p>\u9019\u6a23\u4f60\u5c31\u5b8c\u5168\u628a JSON string \u8f49\u56de\u53bb struct \u4e86\u3002\u6709\u5920\u7c21\u55ae\u5427\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u70ba\u524d PHP \u958b\u767c\u8005\uff0c\u4e00\u8aaa\u5230 Golang \u8981\u628a\u6771\u897f\u8f49\u505a JSON \u5c31\u982d\u75db\uff0c\u56e0\u70ba\u5728 php \u88e1\u9762\u4f60\u53ef\u4ee5\u975e\u5e38\u7c21\u55ae\u7684\u628a\u4e00\u500b\u8907\u96dc\u7684\u7d50\u69cb\u8f49\u63db\u6210 json\uff0c\u4f8b\u5982\u8aaa\uff1a \u5c31\u80fd\u5920\u8f38\u51fa \u6240\u4ee5\u5728\u958b\u767c php \u7684\u6642\u5019\u57fa\u672c\u4e0a\u662f\u4e0d\u7528\u7279\u5225\u8655\u7406 json \u7684\u8f49\u63db\u7684\uff0c\u5230\u4e86 javascript \u4e00\u7aef\u4e5f\u80fd\u5920\u8f15\u6613\u7684\u628a\u6536\u56de\u4f86\u7684\u8cc7\u6599\u76f4\u63a5\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a \u4f46\u662f\u53bb\u5230 Golang \u4e4b\u5f8c\u5beb\u6cd5\u5c31\u5b8c\u5168\u4e0d\u540c\u4e86\uff0c\u5728\u9019\u7bc7\u7c21\u77ed\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u6703\u7c21\u55ae\u4ecb\u7d39\u4e00\u4e0b\u600e\u6a23\u8f15\u6613\u7684\u628a\u4efb\u4f55\u8cc7\u6599 parse \u6210 JSON string \u53ca\u7531 JSON string \u8f49\u56de\u539f\u672c\u578b\u614b\u7684\u65b9\u6cd5 Golang \u7684 Struct \u662f\u4e00\u6a23\u5f88\u795e\u5947\u7684\u6771\u897f \u751a\u9ebc\u662f Golang Struct \u5462\uff1f\u9019\u500b\u8ddf C \u8a9e\u8a00\u88e1\u7684\u6c92\u5dee\u592a\u9060\uff0c\u7c21\u55ae\u4f86\u8aaa\u5c31\u662f Golang \u4e0a OOP \u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Struct \u4f8b\u5b50\uff08\u539f\u78bc\u53d6\u81ea ArOZ Online 1.0\uff09 \u628a Struct \u8f49\u63db\u6210 JSON \u9019\u662f\u4e00\u500b\u7528\u4f86\u5b9a\u7fa9\u684c\u9762\u4e0a\u7684\u7269\u54c1\u7684 struct\u3002\u8981\u628a\u5b83\u8f49\u5230 JSON [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[89,91,90],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-instructions-and-information","tag-golang","tag-json","tag-struct"],"_links":{"self":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":4,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/489\/revisions\/495"}],"wp:attachment":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}