{"id":206952,"date":"2025-07-08T08:37:28","date_gmt":"2025-07-08T00:37:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206952\/"},"modified":"2025-07-08T08:37:28","modified_gmt":"2025-07-08T00:37:28","slug":"%e5%90%88%e5%b9%b6%e7%bb%93%e6%9e%84%e4%bd%93%e5%88%87%e7%89%87","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206952\/","title":{"rendered":"\u5408\u5e76\u7ed3\u6784\u4f53\u5207\u7247"},"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>\u5408\u5e76\u7ed3\u6784\u4f53\u5207\u7247<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-18 23:06: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>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5408\u5e76\u7ed3\u6784\u4f53\u5207\u7247\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5c06\u4e24\u4e2a\u4e0d\u540c\u7684 json \u7ed3\u6784\u6570\u7ec4\u5408\u5e76\u5230\u4e00\u4e2a json blob\uff08\u6765\u81ea\u5355\u72ec\u7684\u5206\u9875\u8bf7\u6c42\uff09\u3002\u4f46\u662f\uff0c\u6211\u65e0\u6cd5\u5c06\u5b83\u4eec\u5408\u5e76\u5728\u4e00\u8d77\uff1a<\/p>\n<pre>package main\n\nimport (\n  \"encoding\/json\"\n  \"log\"\n)\n\nfunc main() {\n  superset := `[{\"id\": 1231, \"name\": \"testing1\"},{\"id\": 1235, \"name\": \"testing2\"}]`\n  subset   := `[{\"id\": 1237, \"name\": \"testing3\"}]`\n\n  s, _ := json.marshal(superset)\n  log.printf(string(s))\n\n  u, _ := json.marshal(subset)\n  log.printf(string(u))\n\n   for i := range s {\n     u = append(u, s[i])\n   }\n\n   log.printf(string(u))\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u8fd9\u5b9e\u9645\u4e0a\u53ea\u662f\u5c06\u5b83\u4eec\u4e32\u8054\u8d77\u6765\uff1a<\/p>\n<pre>\"[{\\\"id\\\": 1237, \\\"name\\\": \\\"testing3\\\"}]\"\"[{\\\"id\\\": 1231, \\\"name\\\": \\\"testing1\\\"},{\\\"id\\\": 1235, \\\"name\\\": \\\"testing2\\\"}]\"<\/pre>\n<p>\u6211\u5e0c\u671b\u5f97\u5230\u5982\u4e0b\u6240\u793a\u7684\u8f93\u51fa\uff1a<\/p>\n<pre>[{\"id\": 1237, \"name\": \"testing3\"},{\"id\": 1231, \"name\": \"testing1\"},{\"id\": 1235, \"name\": \"testing2\"}]<\/pre>\n<p>\u6211\u89c9\u5f97\u6211\u9519\u8fc7\u4e86\u4e00\u4e9b\u611a\u8822\u7684\u4e8b\u60c5\u3002\u4efb\u4f55\u5e2e\u52a9\u5c06\u4e0d\u80dc\u611f\u6fc0\u3002\u8c22\u8c22\uff01<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p> \u5c06\u7ed9\u5b9a\u503c\u7f16\u7ec4\u4e3a json\uff08\u5e76\u5c06\u5176\u4f5c\u4e3a <code>[]byte<\/code> \u8fd4\u56de\uff09\uff0c\u56e0\u6b64 <code>s<\/code> \u548c <code>u<\/code> \u4e2d\u7684\u5185\u5bb9\u662f\u4fdd\u5b58\u8f93\u5165 json \u6587\u672c\u7684 json \u6587\u672c\u3002\u9644\u52a0\uff08\u8fde\u63a5\uff09\u8fd9\u4e24\u4e2a\u6587\u672c\u751a\u81f3\u4e0d\u4f1a\u4ea7\u751f\u6709\u6548\u7684 json\uff0c\u663e\u7136\u4e0d\u662f\u60a8\u60f3\u8981\u7684\u3002<\/p>\n<p>\u60a8\u7684\u8f93\u5165\u662f json \u6587\u672c\uff0c\u5305\u542b\u4e00\u4e2a json \u6570\u7ec4\u3002\u56e0\u6b64\uff0c\u60a8\u5e94\u8be5\u505a\u7684\u662f\u5c06\u8f93\u5165\u89e3\u7ec4\u5230 go \u5207\u7247\uff08\u7c7b\u578b\u4e3a <code>[]interface{}<\/code>\uff09\u4e2d\uff0c\u60a8\u53ef\u4ee5\u9644\u52a0\u8be5\u5207\u7247\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u7f16\u7ec4\u56de json\u3002<\/p>\n<p>\u7c7b\u4f3c\u8fd9\u6837\u7684\u4e8b\u60c5\uff1a<\/p>\n<pre>superset := `[{\"id\": 1231, \"name\": \"testing1\"},{\"id\": 1235, \"name\": \"testing2\"}]`\nsubset := `[{\"id\": 1237, \"name\": \"testing3\"}]`\n\nvar s1, s2 []interface{}\n\nif err := json.unmarshal([]byte(superset), &amp;s1); err != nil {\n    panic(err)\n}\nif err := json.unmarshal([]byte(subset), &amp;s2); err != nil {\n    panic(err)\n}\n\ns2 = append(s2, s1...)\n\nresult, err := json.marshal(s2)\nif err != nil {\n    panic(err)\n}\nfmt.println(string(result))<\/pre>\n<p>\u6b64\u8f93\u51fa\uff08\u5728 \u4e0a\u5c1d\u8bd5\uff09\uff1a<\/p>\n<pre>[{\"id\":1237,\"name\":\"testing3\"},{\"id\":1231,\"name\":\"testing1\"},{\"id\":1235,\"name\":\"testing2\"}]<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5408\u5e76\u7ed3\u6784\u4f53\u5207\u7247\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-206952","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206952","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=206952"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206952\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}