{"id":203426,"date":"2025-05-22T09:17:51","date_gmt":"2025-05-22T01:17:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203426\/"},"modified":"2025-05-22T09:17:51","modified_gmt":"2025-05-22T01:17:51","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%b3%9b%e5%9e%8b%e6%96%b9%e6%b3%95%e5%92%8c-class-%e5%8f%8d%e5%ba%8f%e5%88%97%e5%8c%96-json-%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ba%e6%8c%87%e5%ae%9a%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203426\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\u548c Class \u53cd\u5e8f\u5217\u5316 JSON \u5b57\u7b26\u4e32\u4e3a\u6307\u5b9a\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\u548c Class  \u53cd\u5e8f\u5217\u5316 JSON \u5b57\u7b26\u4e32\u4e3a\u6307\u5b9a\u7c7b\u578b\uff1f <\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\u548c Class  \u53cd\u5e8f\u5217\u5316 JSON \u5b57\u7b26\u4e32\u4e3a\u6307\u5b9a\u7c7b\u578b\uff1f\u300b <\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\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\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731033778672d7ab22c43d.jpg\" class=\"aligncenter\"> \u53cd\u5e8f\u5217\u5316 JSON \u5b57\u7b26\u4e32\u4e3a\u6307\u5b9a\u7c7b\u578b\uff1f&#8221; \/&gt;<\/p>\n<p><strong>\u6cdb\u578b\u7c7b\u65b9\u6cd5\u4e2d\u7684 class&lt;t&gt; \u4f7f\u7528\u89e3\u6790<\/strong><\/p>\n<p>\u6cdb\u578b\u7c7b\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 class&lt;t&gt; \u7c7b\u578b\u53c2\u6570\u6765\u6307\u5b9a\u5b9e\u9645\u7c7b\u578b\u3002\u5728\u53cd\u5e8f\u5217\u5316\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u673a\u5236\u6765\u6307\u5b9a\u8981\u8f6c\u6362\u7684\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<p>\u5728\u7ed9\u5b9a\u7684\u793a\u4f8b\u4e2d\uff0creqbody \u7c7b\u5177\u6709\u4e00\u4e2a\u63a5\u53d7 json \u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002\u4e3a\u4e86\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5 fromjson \u8fdb\u884c\u53cd\u5e8f\u5217\u5316\uff0c\u9700\u8981\u5c06\u76ee\u6807\u7c7b\u578b\u7684 class \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4e3a\u6b64\uff0c\u53ef\u4ee5\u5c06 class&lt;t&gt; \u7c7b\u578b\u53c2\u6570\u6dfb\u52a0\u5230 reqbody \u7c7b\u4e2d\uff1a<\/p>\n<pre>public class reqbody&lt;t&gt; {\n\n    private t parameter;\n\n    \/**\n     * \u4f7f\u7528 json \u53cd\u5e8f\u5217\u5316\u8d4b\u503c\n     *\/\n    public reqbody(string json, class&lt;t&gt; clzz) {\n        parameter = (t) jsonobject.parseobject(json, clzz);\n    }\n}<\/pre>\n<p>\u7136\u540e\uff0c\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528 class \u7c7b\u578b\u53c2\u6570\u6765\u5b9e\u4f8b\u5316\u76ee\u6807\u7c7b\u578b\uff1a<\/p>\n<pre>ReqBody&lt;Parameter&gt; reqBody = new ReqBody&lt;Parameter&gt;(\"{\\\"path\\\":\\\"The path param\\\"}\", Parameter.class);<\/pre>\n<p>\u8fd9\u6837\uff0c\u5373\u53ef\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5 fromjson \u5c06 json \u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u4e3a\u6307\u5b9a\u7684\u76ee\u6807\u7c7b\u578b\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\u548c Class  \u53cd\u5e8f\u5217\u5316 JSON \u5b57\u7b26\u4e32\u4e3a\u6307\u5b9a\u7c7b\u578b\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u65b9\u6cd5\u548c Class &#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-203426","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203426","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=203426"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203426\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}