{"id":66956,"date":"2025-05-03T09:41:28","date_gmt":"2025-05-03T01:41:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66956\/"},"modified":"2025-05-03T09:41:28","modified_gmt":"2025-05-03T01:41:28","slug":"java%e5%af%b9%e8%b1%a1%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2json%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66956\/","title":{"rendered":"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5c06 java \u5bf9\u8c61\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5229\u7528 jackson \u5e93\u521b\u5efa objectmapper\u3002\u8bbe\u7f6e\u8981\u8f6c\u6362\u4e3a json \u7684 java \u5bf9\u8c61\u3002\u4f7f\u7528 writevalueasstring \u65b9\u6cd5\u3002\u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111720364995895.jpg\" class=\"aligncenter\" title=\"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" alt=\"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\uff1f<\/strong><\/p>\n<p>\u8981\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Jackson \u5e93\u521b\u5efa\u4e00\u4e2a ObjectMapper\uff1a<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\nObjectMapper mapper = new ObjectMapper();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u8981\u8f6c\u6362\u4e3a JSON \u7684 Java \u5bf9\u8c61\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] myArray = {\"Value 1\", \"Value 2\", \"Value 3\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 writeValueAsString \u65b9\u6cd5\uff1a<\/strong><\/p>\n<pre>String jsonString = mapper.writeValueAsString(myArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u89e3\u6790 JSON \u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4\uff1a<\/strong><\/p>\n<pre>String[] result = mapper.readValue(jsonString, String[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\npublic class Main {\n  public static void main(String[] args) throws Exception {\n    \/\/ \u521b\u5efa\u8981\u8f6c\u6362\u7684\u6570\u7ec4\n    String[] myArray = {\"Value 1\", \"Value 2\", \"Value 3\"};\n\n    \/\/ \u521b\u5efa ObjectMapper\n    ObjectMapper mapper = new ObjectMapper();\n\n    \/\/ \u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\n    String jsonString = mapper.writeValueAsString(myArray);\n\n    \/\/ \u4ece JSON \u5b57\u7b26\u4e32\u89e3\u6790\u56de\u6570\u7ec4\n    String[] result = mapper.readValue(jsonString, String[].class);\n\n    \/\/ \u6253\u5370\u7ed3\u679c\n    for (String value : result) {\n      System.out.println(value);\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Value 1\nValue 2\nValue 3<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5bf9\u8c61\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>\u8981\u5c06 java \u5bf9\u8c61\u8f6c\u6362\u4e3a json \u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5229\u7528 jackson \u5e93\u521b\u5efa objectmapper\u3002\u8bbe\u7f6e\u8981\u8f6c\u6362\u4e3a json \u7684 java \u5bf9\u8c61\u3002\u4f7f\u7528 writevalueasstring \u65b9\u6cd5\u3002\u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a\u6570\u7ec4\u3002 \u5982\u4f55\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5bf9\u8c61\uff1f \u8981\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u4f7f\u7528 Jackson \u5e93\u521b\u5efa\u4e00\u4e2a ObjectMapper\uff1a import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u8981\u8f6c\u6362\u4e3a JSON \u7684 Java \u5bf9\u8c61\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] myArray = {&#8220;Value 1&#8221;, &#8220;Value 2&#8221;, &#8220;Value 3&#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-66956","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66956","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=66956"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66956\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}