{"id":66875,"date":"2025-05-03T11:23:27","date_gmt":"2025-05-03T03:23:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66875\/"},"modified":"2025-05-03T11:23:27","modified_gmt":"2025-05-03T03:23:27","slug":"%e5%af%b9%e8%b1%a1%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e8%bd%ac%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\/66875\/","title":{"rendered":"\u5bf9\u8c61\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u5bf9\u8c61\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u5728java\u4e2d\uff0c\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a 1. \u4f7f\u7528jackson\u3001gson\u6216org.json\u7b49json\u89e3\u6790\u5e93\u89e3\u6790json\u5b57\u7b26\u4e32\u3002 2.\u5bf9\u4e8ejsonobject\uff0c\u4f7f\u7528getjsonarray()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u952e\u540d\u79f0\u7684\u5bf9\u8c61\u6570\u7ec4\u3002 3.\u5bf9\u4e8ejsonarray\uff0c\u76f4\u63a5\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111713572419484.jpg\" class=\"aligncenter\" title=\"\u5bf9\u8c61\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" alt=\"\u5bf9\u8c61\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u5bf9\u8c61\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<h2>Java \u4e2d\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4<\/h2>\n<p>\u8981\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528 JSON \u89e3\u6790\u5e93\u89e3\u6790 JSON \u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<ul>\n<li>Jackson\u3001Gson \u548c org.json \u662f\u5e38\u7528\u7684 JSON \u89e3\u6790\u5e93\u3002<\/li>\n<li>\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e2d\u7684 parse() \u65b9\u6cd5\u5c06 JSON \u4e3a JSONObject \u6216 JSONArray \u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5bf9\u8c61\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e JSONObject\uff0c\u4f7f\u7528 getJSONArray(String key) \u65b9\u6cd5\u83b7\u53d6\u952e\u4e3a\u6307\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\u6570\u7ec4\u3002<\/li>\n<li>\u5bf9\u4e8e JSONArray\uff0c\u76f4\u63a5\u5c06 JSONArray \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Jackson \u5e93\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\nimport com.fasterxml.jackson.databind.type.TypeFactory;\n\n\/\/ \u5bf9\u8c61\u5b57\u7b26\u4e32\nString objectString = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Jane\", \"age\":25}]\";\n\n\/\/ \u4f7f\u7528 Jackson \u89e3\u6790 JSON \u5b57\u7b26\u4e32\nObjectMapper mapper = new ObjectMapper();\nJSONArray jsonArray = mapper.readValue(objectString, JSONArray.class);\n\n\/\/ \u5c06 JSONArray \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\nObject[] objects = mapper.convertValue(jsonArray, TypeFactory.defaultInstance().constructArrayType(Object.class));\n\n\/\/ \u904d\u5386\u5bf9\u8c61\u6570\u7ec4\nfor (Object object : objects) {\n    System.out.println(object);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5bf9\u8c61\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\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>\u5728java\u4e2d\uff0c\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a 1. \u4f7f\u7528jackson\u3001gson\u6216org.json\u7b49json\u89e3\u6790\u5e93\u89e3\u6790json\u5b57\u7b26\u4e32\u3002 2.\u5bf9\u4e8ejsonobject\uff0c\u4f7f\u7528getjsonarray()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u952e\u540d\u79f0\u7684\u5bf9\u8c61\u6570\u7ec4\u3002 3.\u5bf9\u4e8ejsonarray\uff0c\u76f4\u63a5\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002 Java \u4e2d\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4 \u8981\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u4f7f\u7528 JSON \u89e3\u6790\u5e93\u89e3\u6790 JSON \u5b57\u7b26\u4e32\uff1a Jackson\u3001Gson \u548c org.json \u662f\u5e38\u7528\u7684 JSON \u89e3\u6790\u5e93\u3002 \u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e2d\u7684 parse() \u65b9\u6cd5\u5c06 JSON \u4e3a JSONObject \u6216 JSONArray \u5bf9\u8c61\u3002 \u83b7\u53d6\u5bf9\u8c61\u6570\u7ec4\uff1a \u5bf9\u4e8e JSONObject\uff0c\u4f7f\u7528 getJSONArray(String key) \u65b9\u6cd5\u83b7\u53d6\u952e\u4e3a\u6307\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\u6570\u7ec4\u3002 \u5bf9\u4e8e JSONArray\uff0c\u76f4\u63a5\u5c06 JSONArray \u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 Jackson \u5e93\u5c06\u5bf9\u8c61\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5bf9\u8c61\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; \/\/ \u5bf9\u8c61\u5b57\u7b26\u4e32 String objectString = &#8220;[{&#8220;name&#8221;:&#8221;John&#8221;, &#8220;age&#8221;:30}, {&#8220;name&#8221;:&#8221;Jane&#8221;, &#8220;age&#8221;:25}]&#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-66875","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66875","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=66875"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66875\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}