{"id":39334,"date":"2024-11-26T16:38:41","date_gmt":"2024-11-26T08:38:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39334\/"},"modified":"2024-11-26T16:38:41","modified_gmt":"2024-11-26T08:38:41","slug":"java%e6%80%8e%e4%b9%88%e6%8e%a5%e6%94%b6%e5%89%8d%e7%ab%af%e4%bc%a0%e6%9d%a5%e7%9a%84%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39334\/","title":{"rendered":"java\u600e\u4e48\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 json.parsearray()\u3001@requestbody \u6ce8\u89e3\u3001httpservletrequest \u6216\u6307\u5b9a content-type\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a1. json.parsearray()\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a object[] \u6570\u7ec4\uff1b2. @requestbody \u6ce8\u89e3\uff1a\u5c06\u8bf7\u6c42\u6b63\u6587\u7ed1\u5b9a\u5230\u65b9\u6cd5\u53c2\u6570\uff1b3. httpservletrequest\uff1a\u83b7\u53d6\u8bf7\u6c42\u6b63\u6587\u7684\u8f93\u5165\u6d41\u5e76\u89e3\u6790 json \u5b57\u7b26\u4e32\uff1b4. \u6307\u5b9a content-type\uff1a\u786e\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111606334935597.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java Web \u5f00\u53d1\u4e2d\uff0c\u63a5\u6536\u524d\u7aef\u53d1\u9001\u7684\u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5e38\u89c1\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u4e2d\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 JSON.parseArray()<\/strong><\/p>\n<p>JSON.parseArray() \u65b9\u6cd5\u53ef\u5c06 JSON \u4e3a Object[] \u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Jackson \u6216 Gson \u7b49 JSON \u5e93\u6765\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\nString json = \"[{\"name\": \"John\", \"age\": 25}, {\"name\": \"Jane\", \"age\": 30}]\";\nObjectMapper mapper = new ObjectMapper();\nObject[] objects = mapper.readValue(json, Object[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 @RequestBody \u6ce8\u89e3<\/strong><\/p>\n<p>Spring Boot \u63d0\u4f9b\u4e86 @RequestBody \u6ce8\u89e3\uff0c\u53ef\u5c06\u8bf7\u6c42\u6b63\u6587\u7ed1\u5b9a\u5230\u65b9\u6cd5\u53c2\u6570\u3002@RequestBody \u53ef\u7528\u4e8e\u89e3\u6790 JSON \u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a Object[] \u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>@RequestMapping(value = \"\/objects\", method = RequestMethod.POST)\npublic Object[] receiveObjects(@RequestBody Object[] objects) {\n    \/\/ \u5904\u7406\u5bf9\u8c61\u6570\u7ec4\n    return objects;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 HttpServletRequest<\/strong><\/p>\n<p>HttpServletRequest \u7c7b\u63d0\u4f9b\u4e86 getRequestReader() \u65b9\u6cd5\uff0c\u53ef\u83b7\u53d6\u8bf7\u6c42\u6b63\u6587\u7684\u8f93\u5165\u6d41\u3002\u53ef\u4ee5\u4f7f\u7528 Jackson \u6216 Gson \u7b49 JSON \u5e93\u6765\u89e3\u6790\u8f93\u5165\u6d41\u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a Object[] \u6570\u7ec4\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\nHttpServletRequest request = ...;\nString json = request.getReader().lines().collect(Collectors.joining());\nObjectMapper mapper = new ObjectMapper();\nObject[] objects = mapper.readValue(json, Object[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u6307\u5b9a Content-Type<\/strong><\/p>\n<p>\u5728\u53d1\u9001 JSON \u6570\u7ec4\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u8bf7\u6c42\u5934\u90e8\u4e2d\u6307\u5b9a Content-Type \u6807\u5934\u4e3a &#8220;application\/json&#8221;\u3002\u8fd9\u5c06\u6307\u793a\u670d\u52a1\u5668\u6b63\u5728\u53d1\u9001 JSON \u6570\u636e\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>fetch(\"\/objects\", {\n  method: \"POST\",\n  headers: {\n    \"Content-Type\": \"application\/json\",\n  },\n  body: JSON.stringify([\n    { name: \"John\", age: 25 },\n    { name: \"Jane\", age: 30 },\n  ]),\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 json.parsearray()\u3001@requestbody \u6ce8\u89e3\u3001httpservletrequest \u6216\u6307\u5b9a content-type\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a1. json.parsearray()\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a object[] \u6570\u7ec4\uff1b2. @requestbody \u6ce8\u89e3\uff1a\u5c06\u8bf7\u6c42\u6b63\u6587\u7ed1\u5b9a\u5230\u65b9\u6cd5\u53c2\u6570\uff1b3. httpservletrequest\uff1a\u83b7\u53d6\u8bf7\u6c42\u6b63\u6587\u7684\u8f93\u5165\u6d41\u5e76\u89e3\u6790 json \u5b57\u7b26\u4e32\uff1b4. \u6307\u5b9a content-type\uff1a\u786e \u5982\u4f55\u4f7f\u7528 Java \u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u5bf9\u8c61\u6570\u7ec4 \u5728 Java Web \u5f00\u53d1\u4e2d\uff0c\u63a5\u6536\u524d\u7aef\u53d1\u9001\u7684\u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u4e2a\u5e38\u89c1\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Java \u4e2d\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff1a 1. \u4f7f\u7528 JSON.parseArray() JSON.parseArray() \u65b9\u6cd5\u53ef\u5c06 JSON \u4e3a Object[] \u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Jackson \u6216 Gson \u7b49 JSON \u5e93\u6765\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u793a\u4f8b\uff1a import com.fasterxml.jackson.databind.ObjectMapper; String json = &#8220;[{&#8220;name&#8221;: &#8220;John&#8221;, &#8220;age&#8221;: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-39334","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=39334"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39334\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}