{"id":65219,"date":"2025-05-03T13:40:25","date_gmt":"2025-05-03T05:40:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65219\/"},"modified":"2025-05-03T13:40:25","modified_gmt":"2025-05-03T05:40:25","slug":"json%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e6%88%90%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84-java-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65219\/","title":{"rendered":"json\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528 jackson \u5e93\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a1. \u6dfb\u52a0 jackson \u4f9d\u8d56\u9879\uff1b2. \u521b\u5efa\u4e00\u4e2a pojo \u7c7b\uff1b3. \u4f7f\u7528 objectmapper \u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111212242941989.jpg\" class=\"aligncenter\" title=\"json\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" alt=\"json\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>JSON \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u7684 Java \u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>\u5982\u4f55\u4f7f\u7528 Java \u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 Jackson \u5e93\u6765\u5b9e\u73b0\u6b64\u8f6c\u6362\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u6dfb\u52a0 Jackson \u4f9d\u8d56\u9879<\/strong><\/p>\n<p>\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 Jackson \u5e93\u7684\u4f9d\u8d56\u9879\uff1a<\/p>\n<pre>&lt;dependency&gt;\n    &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\n    &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\n    &lt;version&gt;2.13.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u4e00\u4e2a POJO \u7c7b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a Java \u5bf9\u8c61\u7c7b\uff08POJO\uff09\u6765\u8868\u793a JSON \u5bf9\u8c61\u4e2d\u6570\u636e\u7684\u7ed3\u6784\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>public class Data {\n    private String name;\n    private int age;\n    \/\/ getters and setters\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 ObjectMapper<\/strong><\/p>\n<p>\u4f7f\u7528 ObjectMapper \u7c7b\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1a<\/p>\n<pre>ObjectMapper mapper = new ObjectMapper();\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Jane\", \"age\":25}]\";\n\n\/\/ \u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Data \u5bf9\u8c61\u6570\u7ec4\nData[] objects = mapper.readValue(jsonString, Data[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0cobjects \u5c06\u5305\u542b\u4e00\u4e2a Data \u5bf9\u8c61\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u5bf9\u5e94 JSON \u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86 Jackson \u5e93\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e JSON \u8f6c\u6362\uff0c\u4f8b\u5982 Gson \u548c Jettison\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjson\u600e\u4e48\u8f6c\u6362\u6210\u5bf9\u8c61\u6570\u7ec4 java\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u4f7f\u7528 jackson \u5e93\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a1. \u6dfb\u52a0 jackson \u4f9d\u8d56\u9879\uff1b2. \u521b\u5efa\u4e00\u4e2a pojo \u7c7b\uff1b3. \u4f7f\u7528 objectmapper \u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002 JSON \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u7684 Java \u65b9\u6cd5 \u95ee\u9898\uff1a\u5982\u4f55\u4f7f\u7528 Java \u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1f \u7b54\u6848\uff1a\u53ef\u4ee5\u4f7f\u7528 Jackson \u5e93\u6765\u5b9e\u73b0\u6b64\u8f6c\u6362\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u6dfb\u52a0 Jackson \u4f9d\u8d56\u9879 \u5728\u4f60\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0 Jackson \u5e93\u7684\u4f9d\u8d56\u9879\uff1a &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt; &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt; &lt;version&gt;2.13.2&lt;\/version&gt; &lt;\/dependency&gt; \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u4e00\u4e2a POJO \u7c7b \u521b\u5efa\u4e00\u4e2a Java \u5bf9\u8c61\u7c7b\uff08POJO\uff09\u6765\u8868\u793a JSON \u5bf9\u8c61\u4e2d\u6570\u636e\u7684\u7ed3\u6784\u3002\u4f8b\u5982\uff1a public class Data [&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-65219","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65219","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=65219"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65219\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}