{"id":207746,"date":"2025-07-08T12:04:41","date_gmt":"2025-07-08T04:04:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207746\/"},"modified":"2025-07-08T12:04:41","modified_gmt":"2025-07-08T04:04:41","slug":"%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90%e4%b8%8d%e5%90%8c%e7%b1%bb%e5%9e%8b%e7%9a%84json-rpc%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207746\/","title":{"rendered":"\u5982\u4f55\u89e3\u6790\u4e0d\u540c\u7c7b\u578b\u7684JSON-RPC\u8868"},"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\u89e3\u6790\u4e0d\u540c\u7c7b\u578b\u7684JSON-RPC\u8868<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 11:48: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>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u89e3\u6790\u4e0d\u540c\u7c7b\u578b\u7684JSON-RPC\u8868\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\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u83b7\u53d6\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684 json-rpc \u6587\u4ef6\u4e2d\u7684\u4fe1\u606f\uff1a<\/p>\n<pre>{\n    \"id\": \"foo1\",\n    \"error\": null,\n    \"result\": [\n        {\n            \"key\": [\n                \"hello 1\",\n                1,\n                \"world 1\"\n            ],\n            \"val\": {\n                \"type\": \"static\"\n            }\n        },\n        {\n            \"key\": [\n                \"hello 2\",\n                1,\n                \"world 2\"\n            ],\n            \"val\": {\n                \"type\": \"static\"\n            }\n        }\n    ]\n}<\/pre>\n<p>\u8fd9\u662f\u6211\u7684\u89e3\u6790\u51fd\u6570\uff0ckey\u662f\u5b57\u7b26\u4e32\u8868\uff08\u4e0d\u80fd\u63a5\u53d7int\u7c7b\u578b\uff09\uff1a<\/p>\n<pre>type jsonrpcrsp struct {\n    id     string          `json:\"id\"`\n    error  *string         `json:\"error\"`\n    result json.rawmessage `json:\"result\"`\n}\n\ntype jsonrpcentry_val struct {\n    type     string          `json:\"type\"`\n}\ntype jsonrpcentry struct {\n    key     [3]string          `json:\"key\"`\n    val     jsonrpcentry_val  `json:\"val\"`\n}\n\njsonresult := jsonrpcrsp{}\njson.unmarshal(data, &amp;jsonresult)\nentries := []jsonrpcentry{}\nfor _, val := range jsonresult {\n    json.unmarshal(val.result, &amp;entries)\n}<\/pre>\n<p>\u5982\u4f55\u89e3\u6790\u201ckey\u201d\u8868\uff1f&#8230;\u95ee\u9898\u662f\u6709\u4e0d\u540c\u7684\u7c7b\u578b<\/p>\n<p>\u5173\u952e\u8868\u7ed3\u6784\u662f\uff1a<\/p>\n<pre>[ &lt;string&gt;, &lt;int&gt;, &lt;string&gt;]<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8981\u5728 go \u4e2d\u89e3\u7ec4\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5982\u679c\u9700\u8981\u8bbf\u95ee\u7c7b\u578b\uff0c\u5219\u9700\u8981\u4f7f\u7528\u63a5\u53e3\u548c\u7c7b\u578b\u65ad\u8a00\u3002<\/p>\n<p>\u8fd9\u5bf9\u4f60\u6709\u7528\uff1a<\/p>\n<pre>type Result struct {\n    Key [3]interface{} `json:\"key\"`\n    Val struct {\n        Type string `json:\"type\"`\n    } `json:\"val\"`\n}\n\nmsg := JsonRpcRsp{}\njson.Unmarshal(data, &amp;msg)\n\nvar result []Result\njson.Unmarshal(msg.Result, &amp;result)\n\nfor _, v := range result {\n    key1 := v.Key[0].(string)\n    key2 := v.Key[1].(float64)\n    key3 := v.Key[2].(string)\n\n    fmt.Println(key1, key2, key3)\n}\n<\/pre>\n<p>\u5c06\u4e09\u4e2a\u63a5\u53e3\u65ad\u8a00\u4e3a\u5176\u7c7b\u578b\u540e\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u60a8\u7684\u7528\u4f8b\u8fdb\u4e00\u6b65\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u89e3\u6790\u4e0d\u540c\u7c7b\u578b\u7684JSON-RPC\u8868\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\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-207746","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207746","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=207746"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207746\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}