{"id":207046,"date":"2025-07-08T08:47:28","date_gmt":"2025-07-08T00:47:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207046\/"},"modified":"2025-07-08T08:47:28","modified_gmt":"2025-07-08T00:47:28","slug":"%e5%b0%86%e5%80%bc%e9%99%84%e5%8a%a0%e5%88%b0-c-json-%e5%93%8d%e5%ba%94","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207046\/","title":{"rendered":"\u5c06\u503c\u9644\u52a0\u5230 c.Json \u54cd\u5e94"},"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>\u5c06\u503c\u9644\u52a0\u5230 c.Json \u54cd\u5e94<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 17:48:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c06\u503c\u9644\u52a0\u5230 c.Json \u54cd\u5e94\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\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\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u5c06\u4e00\u4e2a\u503c\u9644\u52a0\u5230\u6211\u7684\u4ee3\u7801\u8fd4\u56de\u7684\u54cd\u5e94\u4e2d\uff0c\u8fd9\u662f\u6211\u5df2\u7ecf\u62e5\u6709\u7684\uff1a<\/p>\n<pre>publishershare := 25 \/\/ i also have this variable that want to append to the returned response.\nc.json(http.statusok, nf) \/\/ nf is a row found and returned from database.\n<\/pre>\n<p>\u8fd9\u4f1a\u8fd4\u56de\u8fd9\u6837\u7684 json\uff1a<\/p>\n<pre>{\n    \"id\": 105324,\n    \"title\": \"test title\",\n    \"last_update\": \"2021-03-10t12:50:37+03:30\",\n    \"created_at\": \"2021-03-10t12:50:36+03:30\",\n    \"updated_at\": \"2021-05-05t05:46:39.352859604z\"\n}\n<\/pre>\n<p>\u6211\u9700\u8981\u4e00\u4e2a\u50cf\u8fd9\u6837\u7684 json \u7ed3\u679c\uff1a<\/p>\n<pre>{\n    \"id\": 105324,\n    \"title\": \"test title\",\n    \"last_update\": \"2021-03-10t12:50:37+03:30\",\n    \"created_at\": \"2021-03-10t12:50:36+03:30\",\n    \"updated_at\": \"2021-05-05t05:46:39.352859604z\",\n    \"publisher_share\": 25 \/\/ i want this to be added.\n}\n<\/pre>\n<p>\u8fd9\u662f\u6211\u5230\u76ee\u524d\u4e3a\u6b62\u6240\u5c1d\u8bd5\u8fc7\u7684\uff0c\u4f46\u5b83\u6539\u53d8\u4e86\u67b6\u6784\u5e76\u4e14\u4e0d\u518d\u5411\u540e\u517c\u5bb9\uff1a<\/p>\n<pre>c.Json(http.StatusOK, gin.H{\"book\": nf, \"publisher_share\": publisherShare})<\/pre>\n<p>\u4f46\u8fd9\u4e0d\u662f\u6211\u60f3\u8981\u7684json\u7ed3\u679c\u3002\u6211\u60f3\u8981 <code>publisher_share<\/code> \u4ee5\u53ca\u5176\u4ed6\u5b57\u6bb5\uff0c\u5c31\u50cf\u6211\u4e0a\u9762\u63d0\u5230\u7684 json \u7ed3\u679c\u4e00\u6837\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u53d1\u5e03\u5546\u5171\u4eab\u6dfb\u52a0\u5230\u4e0e\u4ee5\u4e0b\u76f8\u540c\u7684\u7c7b\u578b<\/p>\n<pre>type nfType struct {\n    \/\/ All of the db row fields\n    PublisherShare int `json:\"publisher_share,omitempty\" db:\"-\"`\n}<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5c06\u503c\u9644\u52a0\u5230 c.Json \u54cd\u5e94\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\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-207046","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207046","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=207046"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207046\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}