{"id":208204,"date":"2025-07-08T15:21:55","date_gmt":"2025-07-08T07:21:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208204\/"},"modified":"2025-07-08T15:21:55","modified_gmt":"2025-07-08T07:21:55","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-json-%e5%93%8d%e5%ba%94%e4%b8%ad%e7%9a%84%e7%89%b9%e5%ae%9a%e6%95%b0%e7%bb%84%e5%80%bc%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%ac%ac%e4%b8%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208204\/","title":{"rendered":"\u5982\u4f55\u5c06 JSON \u54cd\u5e94\u4e2d\u7684\u7279\u5b9a\u6570\u7ec4\u503c\u8f6c\u6362\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\uff1f"},"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>\u5982\u4f55\u5c06 JSON \u54cd\u5e94\u4e2d\u7684\u7279\u5b9a\u6570\u7ec4\u503c\u8f6c\u6362\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 10:45:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5c06 JSON \u54cd\u5e94\u4e2d\u7684\u7279\u5b9a\u6570\u7ec4\u503c\u8f6c\u6362\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\uff08\u6ce8\u610f\uff0c\u6b64\u95ee\u9898\u4e0d\u9700\u8981 dgraph\uff1b\u5b83\u53ea\u662f\u6211\u9047\u5230\u95ee\u9898\u65f6\u4f7f\u7528\u7684\u5de5\u5177\uff09<\/p>\n<p>\u5047\u8bbe\u60a8\u6709\u4e00\u4e9b\u7ed3\u6784<\/p>\n<pre>type accountsettings struct {\n    uid      string `json:\"uid\"`\n    dtype    string `json:\"dgraph.type\"`\n    name     string `json:\"name\"`\n    username string `json:\"username\"`\n}\n\ntype settings struct {\n    uid     string          `json:\"uid\"`\n    dtype   string          `json:\"dgraph.type\"`\n    account accountsettings `json:\"account\"`\n}\n\ntype tag struct {\n    uid   string `json:\"uid\"`\n    dtype string `json:\"dgraph.type\"`\n    label label  `json:\"label\"`\n}\n\ntype user struct {\n    uid      string   `json:\"uid\"`\n    dtype    string   `json:\"dgraph.type\"`\n    settings settings `json:\"settings\"`\n    tags     []tag    `json:\"tags\"`\n}<\/pre>\n<p>\u7136\u540e\u60a8\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u60a8\u7684 dgraph \u6570\u636e\u5e93<\/p>\n<pre>user = user{\n    dtype: \"user\",\n    settings: settings{\n        dtype: \"settings\",\n        account: settingsaccount{\n            dtype: \"settingsaccount\",\n            name: \"billy bob\"\n            username: \"billyboy123\"\n        },\n        tags: []tag{{\n            dtype: \"tag\",\n            label: \"admin\"\n        }, {\n            dtype: \"tag\",\n            label: \"user\"\n        }}\n    },\n}<\/pre>\n<p>\u7136\u540e\u4f60\u67e5\u8be2\u4f60\u7684 dgraph \u6570\u636e\u5e93\uff08\u5047\u8bbe\u4f60\u6709\u7528\u6237\u8282\u70b9\u7684 uid\uff09<\/p>\n<pre>{\n  user(func: uid(\"0x1234\")) {\n    uid\n    settings {\n      account {\n        name\n        username\n      }\n    }\n    tags {\n      label\n    }\n  }\n}<\/pre>\n<p>\u54cd\u5e94\u5c06\u8fd4\u56de\u4e3a<\/p>\n<pre>{\n  \"data\": {\n    \"user\": [\n      {\n        \"uid\": \"0x1234\"\n        \"settings\": [\n          {\n            \"account\": [\n              {\n                \"name\": \"Billy Bob\",\n                \"username\": \"billyboy123\"\n              }\n            ]\n          }\n        ]\n        \"tags\": [\n          {\n            label: \"admin\"\n          },\n          {\n            label: \"user\"\n          },\n        ]\n      }\n    ]\n  }\n}<\/pre>\n<p>\u6240\u4ee5\u95ee\u9898\u662f dgraph \u7684 json \u54cd\u5e94\u5c06\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\u7ed3\u6784\u7c7b\u578b\u7684\u5b57\u6bb5\uff08\u5927\u6982\u662f\u56e0\u4e3a\u53ef\u80fd\u6709\u591a\u4e2a\u8282\u70b9\u6307\u5411\u5355\u4e2a\u5176\u4ed6\u8282\u70b9\uff09\uff0c\u4f46\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u56e0\u6b64\u60a8\u65e0\u6cd5\u7acb\u5373\u7f16\u7ec4\u5b83\u4e0e <code>user<\/code> \u7ed3\u6784\uff08\u56e0\u4e3a <code>user.settings<\/code> \u7684\u7c7b\u578b\u4e3a <code>settings<\/code>\uff0c\u800c\u4e0d\u662f <code>[]settings<\/code>\uff09\u3002<\/p>\n<p>\u4e3a\u4e86\u4f7f\u7528 <code>user<\/code> \u7ed3\u6784\uff08\u6216\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u7684\u7ed3\u6784\uff09\u7f16\u7ec4 dgraph json \u54cd\u5e94\uff0c\u60a8\u4f1a\u505a\u4ec0\u4e48\uff1f<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cjson \u54cd\u5e94\u4e2d\u7684 <code>settings<\/code> \u6570\u7ec4\u5e94\u8be5\u53ea\u662f\u8be5\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46 <code>tags<\/code> \u6570\u7ec4\u4ecd\u5e94\u4fdd\u7559\u4e3a\u6570\u7ec4\uff0c\u56e0\u4e3a\u8fd9\u662f <code>user<\/code> \u7ed3\u6784\u4e2d\u6307\u5b9a\u7684\u5185\u5bb9\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49 unmarshal \u6765\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u5982\u4e0b\u6240\u8ff0\uff1a<\/p>\n<\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u539f\u59cb <code>user<\/code> \u5d4c\u5165\u5230\u4e00\u4e2a\u533f\u540d\u7c7b\u578b\u4e2d\u6765\u4f7f\u7528\u522b\u540d\uff0c\u8be5\u533f\u540d\u7c7b\u578b\u5185\u90e8\u6709\u4e00\u4e2a <code>settings<\/code> \u5207\u7247\uff0c\u60a8\u53ef\u4ee5\u50cf\u8fd9\u6837\u8fdb\u884c umarshal \u64cd\u4f5c\uff1a<\/p>\n<pre>func (u *User) UnmarshalJSON(data []byte) error {\n    type Alias User\n    aux := &amp;struct {\n        Settings []*Settings `json:\"settings\"`\n        *Alias\n    }{\n        Alias: (*Alias)(u),\n    }\n    if err := json.Unmarshal(data, &amp;aux); err != nil {\n        return err\n    }\n    u.Settings = aux.Settings[0]\n    return nil\n}<\/pre>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u5b8c\u6574\u7684\u5de5\u4f5c\u793a\u4f8b\uff08\u60a8\u9700\u8981\u5bf9 <code>accountsettings<\/code> \u4f7f\u7528\u76f8\u540c\u7684\u6280\u5de7\uff09\uff1a<\/p>\n<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06 JSON \u54cd\u5e94\u4e2d\u7684\u7279\u5b9a\u6570\u7ec4\u503c\u8f6c\u6362\u4e3a\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u503c\uff1f\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-208204","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208204","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=208204"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208204\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}