{"id":37489,"date":"2024-11-26T16:01:34","date_gmt":"2024-11-26T08:01:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37489\/"},"modified":"2024-11-26T16:01:34","modified_gmt":"2024-11-26T08:01:34","slug":"java%e4%b8%adjson%e6%80%8e%e4%b9%88%e6%8b%bc%e8%a3%85%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37489\/","title":{"rendered":"java\u4e2djson\u600e\u4e48\u62fc\u88c5\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728java\u4e2d\uff0c\u4f7f\u7528jackson\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u62fc\u63a5json\u6570\u7ec4\uff1a\u521b\u5efaobjectmapper\u548c\u7a7a\u7684json\u6570\u7ec4\u3002\u4f7f\u7528jsonarray.add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5bf9\u8c61\u3002\u4f7f\u7528objectmapper\u7684writevalueasstring()\u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111306211753176.jpg\" class=\"aligncenter\" title=\"java\u4e2djson\u600e\u4e48\u62fc\u88c5\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2djson\u600e\u4e48\u62fc\u88c5\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u62fc\u63a5JSON\u6570\u7ec4<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Jackson\u5e93\u8f7b\u677e\u62fc\u63a5JSON\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u521b\u5efaObjectMapper<\/strong><\/p>\n<p>Mapper objectMapper = new ObjectMapper();<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u521b\u5efa\u7a7aJSON\u6570\u7ec4<\/strong><\/p>\n<p>JSONArray jsonArray = new JSONArray();<\/p>\n<p><strong>3. \u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\uff1a<\/p>\n<ul>\n<li>jsonArray.add(&#8220;string&#8221;);<\/li>\n<li>jsonArray.add(123);<\/li>\n<li>jsonArray.add(new JSONObject());<\/li>\n<\/ul>\n<p><strong>4. \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u8981\u5c06JSONArray\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528ObjectMapper\u7684writeValueAsString\u65b9\u6cd5\uff1a<\/p>\n<p>String jsonString = objectMapper.writeValueAsString(jsonArray);<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>ObjectMapper objectMapper = new ObjectMapper();\nJSONArray jsonArray = new JSONArray();\n\njsonArray.add(\"Alice\");\njsonArray.add(\"Bob\");\njsonArray.add(\"Charlie\");\n\nString jsonString = objectMapper.writeValueAsString(jsonArray);\n\nSystem.out.println(jsonString); \/\/ \u8f93\u51fa: [\"Alice\", \"Bob\", \"Charlie\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5b57\u7b26\u4e32\u5143\u7d20\u7684JSON\u6570\u7ec4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2djson\u600e\u4e48\u62fc\u88c5\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>\u5728java\u4e2d\uff0c\u4f7f\u7528jackson\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u62fc\u63a5json\u6570\u7ec4\uff1a\u521b\u5efaobjectmapper\u548c\u7a7a\u7684json\u6570\u7ec4\u3002\u4f7f\u7528jsonarray.add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5bf9\u8c61\u3002\u4f7f\u7528objectmapper\u7684writevalueasstring()\u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32\u3002 Java\u4e2d\u62fc\u63a5JSON\u6570\u7ec4 \u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Jackson\u5e93\u8f7b\u677e\u62fc\u63a5JSON\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a 1. \u521b\u5efaObjectMapper Mapper objectMapper = new ObjectMapper(); \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u521b\u5efa\u7a7aJSON\u6570\u7ec4 JSONArray jsonArray = new JSONArray(); 3. \u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20 \u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\uff1a jsonArray.add(&#8220;string&#8221;); jsonArray.add(123); jsonArray.add(new JSONObject()); 4. \u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32 \u8981\u5c06JSONArray\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528ObjectMapper\u7684writeValueAsString\u65b9\u6cd5\uff1a String jsonString = objectMapper.writeValueAsString(jsonArray); \u793a\u4f8b\uff1a ObjectMapper objectMapper = new ObjectMapper(); JSONArray jsonArray = new JSONArray(); jsonArray.add(&#8220;Alice&#8221;); jsonArray.add(&#8220;Bob&#8221;); jsonArray.add(&#8220;Charlie&#8221;); String jsonString = objectMapper.writeValueAsString(jsonArray); System.out.println(jsonString); \/\/ \u8f93\u51fa: [&#8220;Alice&#8221;, &#8220;Bob&#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-37489","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37489","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=37489"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37489\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}