{"id":207977,"date":"2025-07-08T13:26:50","date_gmt":"2025-07-08T05:26:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207977\/"},"modified":"2025-07-08T13:26:50","modified_gmt":"2025-07-08T05:26:50","slug":"%e5%88%9b%e5%bb%ba%e5%90%8e%e5%b0%86%e5%80%bc%e9%99%84%e5%8a%a0%e5%88%b0-json-%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207977\/","title":{"rendered":"\u521b\u5efa\u540e\u5c06\u503c\u9644\u52a0\u5230 JSON \u7ed3\u6784"},"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>\u521b\u5efa\u540e\u5c06\u503c\u9644\u52a0\u5230 JSON \u7ed3\u6784<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 15:06:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u521b\u5efa\u540e\u5c06\u503c\u9644\u52a0\u5230 JSON \u7ed3\u6784\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\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728 go \u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a json \u7ed3\u6784\u3002\u8fd9\u662f\u6211\u521b\u5efa\u7ed3\u6784\u5e76\u6dfb\u52a0\u5fc5\u8981\u503c\u7684\u4ee3\u7801\u3002 <\/p>\n<pre>type passport struct{\n        messagetopic string `json:\"message_topic\"`\n        devicename string  `json:\"device_name\"`\n        deviceschema string  `json:\"device_schema\"`\n        deviceid string  `json:\"device_id\"`\n    }\n    type sentdata struct{\n        passport passport `json:\"passport\"`\n        integrationresult string `json:\"integration_result\"`\n        actionlog []string `json:\"action_log\"`\n    }\nresponse := sentdata{\n            passport: passport {\n                messagetopic: \"handshake_reply\",\n                devicename: sensorconfig.passport.devicename, \/\/ignore\n                deviceschema: sensorconfig.passport.deviceschema, \/\/ignore\n                deviceid: \"\",\n            },\n            integrationresult: \"\",\n            actionlog: []string{},\n        }\n        sensorreply, _ := json.marshal(response)<\/pre>\n<p>\u6211\u5e0c\u671b\u80fd\u591f\u5c06\u5b57\u7b26\u4e32\u5143\u7d20\u6dfb\u52a0\u5230 <code>actionlog<\/code> \u6570\u7ec4\u4e2d\u3002\u6211\u8fd9\u6837\u505a\u7684\u65b9\u6cd5\u662f\uff1a<\/p>\n<pre>if sensorconfig.passport.interfaces.wifi != \"true\" &amp;&amp; sensorconfig.passport.interfaces.wifi != \"false\" &amp;&amp; sensorconfig.passport.interfaces.wifi != \"unknown\"{\n        fmt.println(\"wifi: incorrect option\")\n        response.actionlog = append(response.actionlog, \"wifi: incorrect option\")\n    }\nfmt.println(string(sensorreply))<\/pre>\n<p>\u8fd9\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u662f\u5f53\u6211\u6253\u5370\u51fa <code>sensorreply<\/code> \u65f6\uff0c\u6211\u5f97\u5230\uff1a <\/p>\n<pre>{\"passport\":{\"message_topic\":\"handshake_reply\",\"device_name\":\"My RPi\",\"device_schema\":\"sensor\/data\",\"device_id\":\"\"},\"integration_result\":\"\",\"action_log\":[]}<\/pre>\n<p>\u5982\u60a8\u6240\u89c1\uff0c<code>action_log<\/code> \u5b57\u6bb5\u4e3a\u7a7a\u3002\u8fd9\u662f\u5c06\u503c\u9644\u52a0\u5230 json \u7684\u6b63\u786e\u65b9\u6cd5\u5417\uff1f <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u7b54\u6848\u53ea\u662f\u4f55\u65f6\u5c01\u9001\u7ed3\u6784\u7684\u95ee\u9898\u3002\u4e0b\u9762\u5b8c\u6210\u4e86\u4ee3\u7801\u3002 <\/p>\n<pre>if sensorConfig.Passport.Interfaces.Wifi != \"true\" &amp;&amp; sensorConfig.Passport.Interfaces.Wifi != \"false\" &amp;&amp; sensorConfig.Passport.Interfaces.Wifi != \"unknown\"{\n    fmt.Println(\"Wifi: incorrect option\")\n    response.ActionLog = append(response.ActionLog, \"Wifi: incorrect option\")\n}\nsensorReply, err := json.Marshal(response)\nif err != nil {\n    panic(err)\n}\nfmt.Println(string(sensorReply))<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207977","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207977","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=207977"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207977\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}