{"id":207656,"date":"2025-07-08T14:40:23","date_gmt":"2025-07-08T06:40:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207656\/"},"modified":"2025-07-08T14:40:23","modified_gmt":"2025-07-08T06:40:23","slug":"%e6%a3%80%e6%9f%a5-json-%e6%98%af%e5%af%b9%e8%b1%a1%e8%bf%98%e6%98%af%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207656\/","title":{"rendered":"\u68c0\u67e5 JSON \u662f\u5bf9\u8c61\u8fd8\u662f\u6570\u7ec4"},"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>\u68c0\u67e5 JSON \u662f\u5bf9\u8c61\u8fd8\u662f\u6570\u7ec4<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 12:42:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u68c0\u67e5 JSON \u662f\u5bf9\u8c61\u8fd8\u662f\u6570\u7ec4\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>Go \u4e2d\u6709\u6ca1\u6709\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u7ed9\u5b9a\u7684 JSON \u662f\u5bf9\u8c61 <code>{}<\/code> \u8fd8\u662f\u6570\u7ec4 <code>[]<\/code>\uff1f<\/p>\n<p>\u9996\u5148\u60f3\u5230\u7684\u5c31\u662f\u628a<code>json.Unmarshal()<\/code>\u53d8\u6210\u4e00\u4e2a\u63a5\u53e3\uff0c\u7136\u540e\u770b\u770b\u662f\u53d8\u6210\u4e86\u4e00\u4e2amap\uff0c\u8fd8\u662f\u4e00\u4e2amap\u7684\u5207\u7247\u3002\u4f46\u8fd9\u4f3c\u4e4e\u6548\u7387\u5f88\u4f4e\u3002<\/p>\n<p>\u6211\u53ef\u4ee5\u68c0\u67e5\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f <code>{<\/code> \u8fd8\u662f <code>[<\/code> \u5417\uff1f\u6216\u8005\u662f\u5426\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u5df2\u7ecf\u5b58\u5728\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u6d4b <code>[]byte<\/code> \u503c <code>data<\/code> \u4e2d\u7684 json \u6587\u672c\u662f\u5426\u662f\u6570\u7ec4\u6216\u5bf9\u8c61\uff1a<\/p>\n<pre>\/\/ get slice of data with optional leading whitespace removed.\n \/\/ see rfc 7159, section 2 for the definition of json whitespace.\n x := bytes.trimleft(data, \" \\t\\r\\n\")\n\n isarray := len(x) &gt; 0 &amp;&amp; x[0] == '['\n isobject := len(x) &gt; 0 &amp;&amp; x[0] == '{'<\/pre>\n<p>\u6b64\u4ee3\u7801\u7247\u6bb5\u5904\u7406\u53ef\u9009\u7684\u524d\u5bfc\u7a7a\u683c\uff0c\u5e76\u4e14\u6bd4\u89e3\u7ec4\u6574\u4e2a\u503c\u66f4\u6709\u6548\u3002<\/p>\n<p>\u7531\u4e8e json \u4e2d\u7684\u9876\u7ea7\u503c\u4e5f\u53ef\u4ee5\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u6216 nil\uff0c\u56e0\u6b64 <code>isarray<\/code> \u548c <code>isobject<\/code> \u7684\u8ba1\u7b97\u7ed3\u679c\u53ef\u80fd\u90fd\u4e3a false\u3002\u5f53 json \u65e0\u6548\u65f6\uff0c\u503c <code>isarray<\/code> \u548c <code>isobject<\/code> \u4e5f\u53ef\u4ee5\u8ba1\u7b97\u4e3a false\u3002<\/p>\n<p>\u4f7f\u7528\u7c7b\u578b\u5f00\u5173\u6765\u786e\u5b9a\u7c7b\u578b\u3002\u8fd9\u4e0e xay \u7684\u7b54\u6848\u7c7b\u4f3c\uff0c\u4f46\u66f4\u7b80\u5355\uff1a<\/p>\n<pre>var v interface{}\nif err := json.Unmarshal(data, &amp;v); err != nil {\n    \/\/ handle error\n}\nswitch v := v.(type) {\ncase []interface{}:\n    \/\/ it's an array\ncase map[string]interface{}:\n    \/\/ it's an object\ndefault:\n    \/\/ it's something else\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u68c0\u67e5 JSON \u662f\u5bf9\u8c61\u8fd8\u662f\u6570\u7ec4\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-207656","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207656","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=207656"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207656\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}