{"id":207683,"date":"2025-07-08T08:35:34","date_gmt":"2025-07-08T00:35:34","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207683\/"},"modified":"2025-07-08T08:35:34","modified_gmt":"2025-07-08T00:35:34","slug":"%e5%9c%a8%e7%bc%96%e7%bb%84%e5%88%b0-json-%e4%b9%8b%e5%89%8d%e5%b0%86%e7%bb%93%e6%9e%84%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%b8%8d%e5%90%8c%e7%9a%84%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207683\/","title":{"rendered":"\u5728\u7f16\u7ec4\u5230 JSON \u4e4b\u524d\u5c06\u7ed3\u6784\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u7ed3\u6784"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u7f16\u7ec4\u5230 JSON \u4e4b\u524d\u5c06\u7ed3\u6784\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u7ed3\u6784<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 16:57:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u7f16\u7ec4\u5230 JSON \u4e4b\u524d\u5c06\u7ed3\u6784\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u7ed3\u6784\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 go \u4f5c\u4e3a\u540e\u7aef\u6765\u5b9e\u73b0\u4e00\u4e2a\u68cb\u76d8\u6e38\u620f\uff0c\u4f46\u9047\u5230\u4e86\u4e00\u4e9b\u969c\u788d\uff0c\u6211\u8bd5\u56fe\u4e86\u89e3\u5728 go \u4e2d\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u590d\u6742\u7684\u7ed3\u6784\uff0c\u5b83\u4ee3\u8868\u6211\u5728\u6e38\u620f\u5f15\u64ce\u4e2d\u4f7f\u7528\u7684\u6e38\u620f\u72b6\u6001\uff0c\u7528\u4e8e\u8bc4\u4f30\u72b6\u6001\u4ee5\u53ca\u6bcf\u4e2a\u52a8\u4f5c\u5c06\u6267\u884c\u7684\u64cd\u4f5c\u4ee5\u53ca\u5b83\u5c06\u5982\u4f55\u5f71\u54cd\u6e38\u620f\u3002<\/p>\n<pre>type game struct {\n    id            string\n    name          string              `json:\"name\"`\n    version       version             `json:\"version\"`\n    startingmode  startingmode        `json:\"startingmode\"`\n    players       []*player           `json:\"players\"`\n    gamemap       *boardmap           `json:\"gamemap\"`\n    countries     map[string]*country `json:\"countries\"`\n    config        *config             `json:\"config\"`\n    turnplayer    *player             `json:\"turnplayer\"`    \/\/represents the player who started the turn\n    turncountry   *country            `json:\"turncountry\"`   \/\/represents the country which started the turn\n    currentplayer *player             `json:\"currentplayer\"` \/\/represents the current player to action\n    gamestart    bool     `json:\"gamestart\"`\n    gamefinish   bool     `json:\"gamefinish\"`\n    inauction    bool     `json:\"inauction\"` \/\/represents the game is in the auction stage\n    gamesettings settings `json:\"settings\"`\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u53ef\u4ee5\u5c06\u5176\u7f16\u7ec4\u4e3a json \u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u6211\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u5b83\u5de5\u4f5c\u6b63\u5e38\uff0c\u4f46\u662f\u5f53\u6211\u5fc5\u987b\u5c06\u5176\u53d1\u9001\u5230\u524d\u7aef\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4e0d\u8d77\u4f5c\u7528\u3002\u800c\u4e14\u524d\u7aef\u4e0d\u9700\u8981\u77e5\u9053\u90a3\u4e48\u591a\u4fe1\u606f\uff0c\u6211\u771f\u7684\u60f3\u8981\u4e00\u4e9b\u66f4\u7b80\u5355\u7684\u4e1c\u897f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>type OtherGame struct {\n players []*OtherPlayer\n countries []*OtherCountry\n map []*OtherArea\n}<\/pre>\n<p>\u6240\u4ee5\uff0c\u6211\u60f3\u6211\u5fc5\u987b\u7f16\u5199\u4e00\u4e9b\u8f6c\u6362\u5668\u51fd\u6570\uff0c\u7136\u540e\u5c01\u9001\u8fd9\u4e2a othergame \u7ed3\u6784\uff0c\u6216\u8005<strong>\u6211\u5e94\u8be5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8fed\u4ee3 game \u4e2d\u7684\u4e0d\u540c\u7ed3\u6784\u5e76\u4f7f\u7528 sprintf \u5c06\u5b83\u4eec\u653e\u5165\u5b57\u7b26\u4e32\u4e2d\uff1f<\/strong><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u7ecf\u5e38\u4f7f\u7528\u8fd9\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u6765\u4e3a\u7279\u5b9a\u5904\u7406\u7a0b\u5e8f\u751f\u6210\u81ea\u5b9a\u4e49\u8f93\u51fa\u3002\u5728\u90a3\u91cc\u5b9a\u4e49\u60a8\u7684 json \u6807\u7b7e\u5e76\u4ec5\u516c\u5f00\u60a8\u9700\u8981\u7684\u5185\u5bb9\u3002\u7136\u540e\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e0e\u60a8\u7684\u5904\u7406\u7a0b\u5e8f\u7d27\u5bc6\u8026\u5408\uff1a<\/p>\n<pre>func gamehandler(w http.responsewriter, r *http.request) {\n    g, err := dblookupgame(r) \/\/ handle err\n\n    \/\/ define one-time anonymous struct for custom formatting\n    jv := struct {\n        id      string `json:\"id\"`\n        name    string `json:\"name\"`\n        version string `json:\"ver\"`\n    }{\n        id:      g.id,\n        name:    g.name,\n        version: g.version,\n    }\n\n    err = json.newencoder(w).encode(&amp;jv) \/\/ handle err\n}\n\ntype game struct {\n    id      string\n    name    string\n    version string\n    secret  string \/\/ don't expose this\n   \/\/ other internals ...\n}<\/pre>\n<\/p>\n<p>\u60a8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a <code>method<\/code> \u5c06 <code>game<\/code> \u6570\u636e\u8f6c\u6362\u4e3a <code>othergame<\/code>\u3002\u50cf\u8fd9\u6837\u7684\u4e1c\u897f\u3002<\/p>\n<pre>func (game game) othergame() othergame {\n    return othergame{\n        players:   game.players,\n        countries: game.countries,\n    }\n}\n<\/pre>\n<p>\u5728\u53d1\u9001\u5230 <code>front-end<\/code> \u4e4b\u524d\u8c03\u7528\u6b64\u65b9\u6cd5\u3002<\/p>\n<pre>game := Game{...}\notherGame := game.OtherGame()\n<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5728\u7f16\u7ec4\u5230 JSON \u4e4b\u524d\u5c06\u7ed3\u6784\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u7ed3\u6784\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207683","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207683","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=207683"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207683\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}