{"id":10006068,"date":"2022-08-01T12:02:58","date_gmt":"2022-08-01T04:02:58","guid":{"rendered":"https:\/\/ityet.com\/?p=10006068"},"modified":"2022-08-01T12:02:58","modified_gmt":"2022-08-01T04:02:58","slug":"%e7%94%a8%e4%bd%8d%e8%bf%90%e7%ae%97%e4%b8%ba%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%8a%a0%e9%80%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006068","title":{"rendered":"\u7528\u4f4d\u8fd0\u7b97\u4e3a\u4f60\u7684\u7a0b\u5e8f\u52a0\u901f"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>\u6700\u8fd1\u5728\u6301\u7eed\u4f18\u5316\u4e4b\u524d\u7f16\u5199\u7684 <code>JSON<\/code> \u89e3\u6790\u5e93 <a href=\"https:\/\/link.segmentfault.com\/?enc=A3%2FM%2FGW2jjdvguSINU%2FJ%2Bw%3D%3D.XaC0x2uacLS8ej7wu4Bm1kb33AEiokfTeaTfc%2FBN8x3NZ%2FacyJxOHJeKYt0SQtJ8\" rel=\"nofollow\">xjson<\/a>\uff0c\u4e3b\u8981\u662f\u4e24\u4e2a\u65b9\u9762\u7684\u4f18\u5316\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u662f\u652f\u6301\u5c06\u4e00\u4e2a <code>JSONObject<\/code> \u5bf9\u8c61\u8f93\u51fa\u4e3a <code>JSON<\/code> \u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8fd9\u70b9\u5728\u4e0a\u4e2a\u7248\u672c\u4e2d\u53ea\u662f\u5229\u7528\u81ea\u5e26\u7684 <code>Print<\/code> \u51fd\u6570\u6253\u5370\u6570\u636e\uff1a<\/p>\n<pre><code class=\"go\">func TestJson4(t *testing.T)  {\n    str := `{&quot;people&quot;:{&quot;name&quot;:{&quot;first&quot;:&quot;bob&quot;}}}`\n    first := xjson.Get(str, &quot;people.name.first&quot;)\n    assert.Equal(t, first.String(), &quot;bob&quot;)\n    get := xjson.Get(str, &quot;people&quot;)\n    fmt.Println(get.String())\n    \/\/assert.Equal(t, get.String(),`{&quot;name&quot;:{&quot;first&quot;:&quot;bob&quot;}}`)\n}<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code class=\"shell\">map[name:map[first:bob]]<\/code><\/pre>\n<p>&lt;!&#8211;more&#8211;&gt;<\/p>\n<p>\u672c\u6b21\u4f18\u5316\u4e4b\u540e\u4fbf\u80fd\u76f4\u63a5\u8f93\u51fa JSON \u5b57\u7b26\u4e32\u4e86\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042250545\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b9e\u73b0\u8fc7\u7a0b\u4e5f\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u9012\u5f52\u904d\u5386 object \u4e2d\u7684\u6570\u636e\uff0c\u7136\u540e\u62fc\u63a5\u5b57\u7b26\u4e32\u5373\u53ef\uff0c\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">func (r Result) String() string {\n    switch r.Token {\n    case String:\n        return fmt.Sprint(r.object)\n    case Bool:\n        return fmt.Sprint(r.object)\n    case Number:\n        i, _ := strconv.Atoi(fmt.Sprint(r.object))\n        return fmt.Sprintf(&quot;%d&quot;, i)\n    case Float:\n        i, _ := strconv.ParseFloat(fmt.Sprint(r.object), 64)\n        return fmt.Sprintf(&quot;%f&quot;, i)\n    case JSONObject:\n        return object2JSONString(r.object)\n    case ArrayObject:\n        return object2JSONString(r.Array())\n    default:\n        return &quot;&quot;\n    }\n}<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042250546\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u7528\u4f4d\u8fd0\u7b97\u4f18\u5316<\/h2>\n<p>\u7b2c\u4e8c\u4e2a\u4f18\u5316\u4e3b\u8981\u662f\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u67e5\u8be2\u4e00\u4e2a\u590d\u6742 JSON \u6570\u636e\u7684\u65f6\u5019\u6027\u80fd\u63d0\u9ad8\u4e86\u5927\u7ea6 \u23eb16%.<\/p>\n<pre><code class=\"shell\"># \u4f18\u5316\u524d\nBenchmarkDecode-12         90013             66905 ns\/op           42512 B\/op       1446 allocs\/op\n\n# \u4f18\u5316\u540e\nBenchmarkDecode-12        104746             59766 ns\/op           37749 B\/op       1141 allocs\/op<\/code><\/pre>\n<p>\u8fd9\u91cc\u622a\u53d6\u4e86\u4e00\u4e9b\u91cd\u70b9\u6539\u52a8\u7684\u90e8\u5206\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042250547\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5728 JSON \u89e3\u6790\u8fc7\u7a0b\u4e2d\u4f1a\u6709\u4e00\u4e2a\u6709\u9650\u72b6\u6001\u673a\u72b6\u6001\u8fc1\u79fb\u7684\u8fc7\u7a0b\uff0c\u800c\u8fc1\u79fb\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u4e2a\u72b6\u6001\u3002<\/p>\n<p>\u6bd4\u5982\u5f53\u524d\u89e3\u6790\u5230\u7684 token \u503c\u4e3a <code>{<\/code>\uff0c\u90a3\u5b83\u63a5\u4e0b\u6765\u7684 token \u53ef\u80fd\u4f1a\u4e3a <code>ObjectKey:&quot;name&quot;<\/code>,\u4e5f\u53ef\u80fd\u4f1a\u662f <code>BeginObject:{<\/code>,\u5f53\u7136\u4e5f\u53ef\u80fd\u4f1a\u662f <code>EndObject:}<\/code>\uff0c<br \/>\u6240\u4ee5\u5728\u4f18\u5316\u4e4b\u524d\u6211\u662f\u5c06\u72b6\u6001\u5168\u90e8\u5b58\u653e\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\uff0c\u5728\u89e3\u6790\u8fc7\u7a0b\u4e2d\u5982\u679c\u53d1\u73b0\u72b6\u6001\u4e0d\u6ee1\u8db3\u9884\u671f\u7684\u5217\u8868\u65f6\u5219\u4f1a\u629b\u51fa\u8bed\u6cd5\u5f02\u5e38\u7684\u9519\u8bef\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042250548\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\u4f18\u5316\u4e4b\u524d\u662f\u904d\u5386\u8fd9\u4e2a\u96c6\u5408\u6765\u8fdb\u884c\u5224\u65ad\u7684\uff0c\u8fd9\u6837\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <code>O(N)<\/code>,\u4f46\u5f53\u6211\u4eec\u6362\u6210\u4f4d\u8fd0\u7b97\u5c31\u4e0d\u4e00\u6837\u4e86\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u76f4\u63a5\u5c31\u53d8\u4e3a<code>O(1)<\/code>\u4e86\uff0c\u540c\u65f6\u8fd8\u8282\u7701\u4e86\u4e00\u4e2a\u5207\u7247\u7684\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n<p>\u6211\u4eec\u7b80\u5355\u6765\u5206\u6790\u4e0b\u8fd9\u4e2a\u4f4d\u8fd0\u7b97\u4e3a\u4ec0\u4e48\u4f1a\u8fbe\u5230\u5224\u65ad\u4e00\u4e2a\u6570\u636e\u662f\u5426\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u540c\u6837\u7684\u6548\u679c\u3002<\/p>\n<p>\u9996\u5148\u4ee5\u8fd9\u4e24\u4e2a\u72b6\u6001\u4e3a\u4f8b\uff1a<\/p>\n<pre><code class=\"go\">    StatusObjectKey   status = 0x0002\n    StatusColon       status = 0x0004<\/code><\/pre>\n<p>\u4ed6\u4eec\u5206\u522b\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4e3a\uff1a<\/p>\n<pre><code class=\"go\">    StatusObjectKey   status = 0x0002 \/\/0010\n    StatusColon       status = 0x0004 \/\/0100<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u5bf9\u8fd9\u4e24\u4e2a\u6570\u636e\u6c42 <code>|<\/code> \u8fd0\u7b97\u5f97\u5230\u7684\u6570\u636e\u662f <code>0110<\/code>\uff1a<\/p>\n<pre><code class=\"go\">A:0010\nB:0100\n\nC:0110<\/code><\/pre>\n<p>\u8fd9\u65f6\u5019\u5982\u4f55\u6211\u4eec\u5982\u679c\u7528\u8fd9\u4e24\u4e2a\u539f\u59cb\u6570\u636e\u4e0e <code>C:0110<\/code> \u505a <code>&amp;<\/code> \u8fd0\u7b97\u65f6\u5c31\u4f1a\u8fd8\u539f\u4e3a\u521a\u624d\u7684\u4e24\u4e2a\u6570\u636e\u3002<\/p>\n<pre><code class=\"go\">\/\/ input:\nA:0010\nC:0110\n\n\/\/ output:\nA:0010\n\n----------\n\/\/ input:\nB:0100\nC:0110\n\n\/\/ output:\nB:0100<\/code><\/pre>\n<p>\u4f46\u6211\u4eec\u6362\u4e00\u4e2a D \u4e0e C \u6c42 <code>&amp;<\/code> \u65f6\uff1a<\/p>\n<pre><code class=\"go\">D: 1000 \/\/ 0x0008 \u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u4e3a 1000\nC: 0110\nD':0000<\/code><\/pre>\n<p>\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a 0 \u503c\uff0c\u53ea\u8981\u5f97\u51fa\u7684\u6570\u636e\u5927\u4e8e 0 \u6211\u4eec\u5c31\u80fd\u5224\u65ad\u4e00\u4e2a\u6570\u636e\u662f\u5426\u5728\u7ed9\u5b9a\u7684\u96c6\u5408\u4e2d\u4e86\u3002<\/p>\n<blockquote><p>\u5f53\u7136\u8fd9\u91cc\u6709\u4e00\u4e2a\u524d\u63d0\u6761\u4ef6\u5c31\u662f\uff0c\u6211\u4eec\u8f93\u5165\u7684\u6570\u636e\u9ad8\u4f4d\u6c38\u8fdc\u90fd\u662f\u662f 1 \u624d\u884c\uff0c\u4e5f\u5c31\u662f2\u7684\u5e42\u3002<\/p><\/blockquote>\n<p>\u540c\u6837\u7684\u4f18\u5316\u5728\u89e3\u6790\u67e5\u8be2\u8bed\u6cd5\u65f6\u4e5f\u6709\u4f7f\u7528\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042250549\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u5176\u4ed6\u5947\u6deb\u5de7\u6280<\/h2>\n<p>\u5f53\u7136\u4f4d\u8fd0\u7b97\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u6280\u5de7\uff0c\u6bd4\u5982\u5224\u65ad\u5947\u5076\u6570\uff1a<\/p>\n<pre><code class=\"go\">\/\/ \u5076\u6570\na &amp; 1 == 0\n\n\/\/ \u5947\u6570\na &amp; 1 == 1<\/code><\/pre>\n<p>\u4e58\u6cd5\u548c\u9664\u6cd5\uff0c\u53f3\u79fb1\u4e00\u4f4d\u662f\u9664\u4ee52\uff0c\u5de6\u79fb\u4e00\u4f4d\u662f\u4e58\u4ee52.<\/p>\n<pre><code class=\"go\">x := 2\nfmt.Println(x&gt;&gt;1) \/\/1\nfmt.Println(x&lt;&lt;1) \/\/4<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4f4d\u8fd0\u7b97\u5728\u5e26\u6765\u7a0b\u5e8f\u6027\u80fd\u63d0\u5347\u7684\u540c\u65f6\u4e5f\u964d\u4f4e\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u6240\u4ee5\u6211\u4eec\u5f97\u6309\u9700\u9009\u62e9\u662f\u5426\u4f7f\u7528\uff1b<\/p>\n<p>\u518d\u4e00\u4e9b\u5e95\u5c42\u5e93\u3001\u6846\u67b6\u4ee3\u7801\u5bf9\u6027\u80fd\u6709\u6781\u81f4\u8ffd\u6c42\u7684\u573a\u666f\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u5bf9\u6570\u636e\u505a\u52a0\u51cf\u4e58\u9664\u5c31\u6ca1\u5fc5\u8981\u7528\u4f4d\u8fd0\u7b97\u4e86\uff0c\u53ea\u4f1a\u8ba9\u540e\u7eed\u7684\u7ef4\u62a4\u8005\u4e00\u8138\u61f5\u903c\u3002<\/p>\n<p>\u76f8\u5173\u4ee3\u7801\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=YbPhReATiWhSLhKf8mPXAg%3D%3D.AFs83raqLR96FQKrEkc0mOFvwKX7txVXvcs6t%2B2b9grA8hFGRSAQzyH076he99VE\" rel=\"nofollow\">https:\/\/github.com\/crossoverJie\/xjson<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042250543\">https:\/\/segmentfault.com\/a\/1190000042250543<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u6700\u8fd1\u5728\u6301\u7eed\u4f18\u5316\u4e4b\u524d\u7f16\u5199\u7684 JSON \u89e3\u6790\u5e93 xjson\uff0c\u4e3b\u8981\u662f\u4e24\u4e2a\u65b9\u9762\u7684\u4f18\u5316\u3002 \u7b2c\u4e00\u4e2a\u662f\u652f\u6301\u5c06\u4e00\u4e2a JS [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006068","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006068"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006068\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}