{"id":34843,"date":"2024-11-26T09:06:32","date_gmt":"2024-11-26T01:06:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34843\/"},"modified":"2024-11-26T09:06:32","modified_gmt":"2024-11-26T01:06:32","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","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34843\/","title":{"rendered":"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528objectmapper\u5e8f\u5217\u5316java\u5bf9\u8c61\u4e3ajson\u5b57\u7b26\u4e32\uff0c\u518d\u4f7f\u7528jsonparser\u89e3\u6790json\u5b57\u7b26\u4e32\u4e3ajsonarray\uff0c\u6700\u540e\u904d\u5386jsonarray\u4e2d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684java\u5bf9\u8c61\u7c7b\u578b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110304272377630.jpg\" class=\"aligncenter\" title=\"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON <\/strong><\/p>\n<p>\u8981\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5e8f\u5217\u5316\u5bf9\u8c61\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 ObjectMapper \u5c06 Java \u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>ObjectMapper mapper = new ObjectMapper();\nString jsonString = mapper.writeValueAsString(object);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u89e3\u6790\u5b57\u7b26\u4e32\u4e3a JSON \u6570\u7ec4\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 JSONParser \u5c06 JSON \u4e3a JSON \u6570\u7ec4\u3002<\/p>\n<pre>JSONParser parser = new JSONParser();\nJSONArray jsonArray = (JSONArray) parser.parse(jsonString);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8f6c\u6362\u6570\u7ec4\u5143\u7d20\uff1a<\/strong><\/p>\n<p>\u904d\u5386 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684 Java \u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<pre>for (Object element : jsonArray) {\n    \/\/ \u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u5982\u4f55\u5c06\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684 Java \u5bf9\u8c61\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u6570\u7ec4\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa Java \u5bf9\u8c61\u6570\u7ec4\nString[] objects = {\"Hello\", \"World\", \"!\"};\n\n\/\/ \u5e8f\u5217\u5316\u5bf9\u8c61\u6570\u7ec4\u4e3a JSON \u5b57\u7b26\u4e32\nObjectMapper mapper = new ObjectMapper();\nString jsonString = mapper.writeValueAsString(objects);\n\n\/\/ \u89e3\u6790 JSON \u5b57\u7b26\u4e32\u4e3a JSON \u6570\u7ec4\nJSONParser parser = new JSONParser();\nJSONArray jsonArray = (JSONArray) parser.parse(jsonString);\n\n\/\/ \u904d\u5386 JSON \u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5e76\u6253\u5370\nfor (Object element : jsonArray) {\n    System.out.println(element);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd0\u884c\u6b64\u4ee3\u7801\u6bb5\u5c06\u6253\u5370\uff1a<\/p>\n<pre>Hello\nWorld\n!<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5bf9\u8c61\u600e\u4e48\u8f6c\u6362json\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>\u53ef\u4ee5\u4f7f\u7528objectmapper\u5e8f\u5217\u5316java\u5bf9\u8c61\u4e3ajson\u5b57\u7b26\u4e32\uff0c\u518d\u4f7f\u7528jsonparser\u89e3\u6790json\u5b57\u7b26\u4e32\u4e3ajsonarray\uff0c\u6700\u540e\u904d\u5386jsonarray\u4e2d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684java\u5bf9\u8c61\u7c7b\u578b\u3002 \u5982\u4f55\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u8981\u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5e8f\u5217\u5316\u5bf9\u8c61\uff1a \u4f7f\u7528 ObjectMapper \u5c06 Java \u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a JSON \u5b57\u7b26\u4e32\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(object); \u767b\u5f55\u540e\u590d\u5236 2. \u89e3\u6790\u5b57\u7b26\u4e32\u4e3a JSON \u6570\u7ec4\uff1a \u4f7f\u7528 JSONParser \u5c06 JSON \u4e3a JSON \u6570\u7ec4\u3002 JSONParser parser = new JSONParser(); JSONArray jsonArray = (JSONArray) parser.parse(jsonString); \u767b\u5f55\u540e\u590d\u5236 3. \u8f6c\u6362\u6570\u7ec4\u5143\u7d20\uff1a \u904d\u5386 [&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-34843","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34843","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=34843"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34843\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}