{"id":39372,"date":"2024-11-26T12:23:00","date_gmt":"2024-11-26T04:23:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39372\/"},"modified":"2024-11-26T12:23:00","modified_gmt":"2024-11-26T04:23:00","slug":"%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e8%bd%ac%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39372\/","title":{"rendered":"\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u5bf9\u8c61\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 objectmapper\uff0c\u53ef\u4ee5\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u5305\u62ec javabean \u6570\u7ec4\u3001map \u6570\u7ec4\u548c\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\u3002\u6b65\u9aa4\u4e3a\uff1a1. \u521b\u5efa objectmapper \u5bf9\u8c61\uff1b2. \u8bfb\u53d6 json \u5b57\u7b26\u4e32\uff1b3. \u4f7f\u7528 readvalue() \u65b9\u6cd5\u8f6c\u6362\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111607011680892.jpg\" class=\"aligncenter\" title=\"\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1f<\/strong><\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 ObjectMapper \u7c7b\u4ece Jackson \u6838\u5fc3\u5e93\u4e2d\u8f6c\u6362 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa ObjectMapper \u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>ObjectMapper mapper = new ObjectMapper();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u8bfb\u53d6 JSON \u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>String jsonString = \"{ \"key1\": \"value1\", \"key2\": \"value2\" }\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>JavaBean \u7c7b\uff08\u5bf9\u8c61\uff09\uff1a<\/p>\n<pre>MyBean[] beans = mapper.readValue(jsonString, MyBean[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>Map \u7c7b\uff1a<\/p>\n<pre>Map[] maps = mapper.readValue(jsonString, Map[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\uff1a<\/p>\n<pre>int[] ints = mapper.readValue(jsonString, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a JavaBean \u6570\u7ec4\uff1a<\/p>\n<pre>public class MyBean {\n    private String key1;\n    private String key2;\n    \/\/ \u7701\u7565 getter \u548c setter\n}\n\nObjectMapper mapper = new ObjectMapper();\nMyBean[] beans = mapper.readValue(jsonString, MyBean[].class);\nfor (MyBean bean : beans) {\n    System.out.println(bean.getKey1() + \" : \" + bean.getKey2());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>value1 : value2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u5bf9\u8c61\u6570\u7ec4 java\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>\u901a\u8fc7 objectmapper\uff0c\u53ef\u4ee5\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u5305\u62ec javabean \u6570\u7ec4\u3001map \u6570\u7ec4\u548c\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\u3002\u6b65\u9aa4\u4e3a\uff1a1. \u521b\u5efa objectmapper \u5bf9\u8c61\uff1b2. \u8bfb\u53d6 json \u5b57\u7b26\u4e32\uff1b3. \u4f7f\u7528 readvalue() \u65b9\u6cd5\u8f6c\u6362\u3002 \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1f \u65b9\u6cd5\uff1a \u4f7f\u7528 ObjectMapper \u7c7b\u4ece Jackson \u6838\u5fc3\u5e93\u4e2d\u8f6c\u6362 JSON \u5b57\u7b26\u4e32\u3002 \u6b65\u9aa4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa ObjectMapper \u5bf9\u8c61\uff1a ObjectMapper mapper = new ObjectMapper(); \u767b\u5f55\u540e\u590d\u5236 \u8bfb\u53d6 JSON \u5b57\u7b26\u4e32\uff1a String jsonString = &#8220;{ &#8220;key1&#8221;: &#8220;value1&#8221;, &#8220;key2&#8221;: &#8220;value2&#8243; }&#8221;; \u767b\u5f55\u540e\u590d\u5236 \u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1a JavaBean \u7c7b\uff08\u5bf9\u8c61\uff09\uff1a MyBean[] [&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-39372","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39372","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=39372"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39372\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}