{"id":66004,"date":"2025-05-03T09:57:25","date_gmt":"2025-05-03T01:57:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66004\/"},"modified":"2025-05-03T09:57:25","modified_gmt":"2025-05-03T01:57:25","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9a%e4%b9%89json%e6%95%b0%e7%bb%84%e7%9a%84%e9%95%bf%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66004\/","title":{"rendered":"java\u600e\u4e48\u5b9a\u4e49json\u6570\u7ec4\u7684\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528jackson\u5e93\u7684arraynode\uff1a\u521b\u5efaarraynode\uff0c\u6dfb\u52a0\u5143\u7d20\u5e76\u8c03\u7528size()\u83b7\u53d6\u957f\u5ea6\u3002\u4f7f\u7528gson\u5e93\u7684jsonarray\uff1a\u521b\u5efajsonarray\uff0c\u6dfb\u52a0\u5143\u7d20\u5e76\u8c03\u7528size()\u83b7\u53d6\u957f\u5ea6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410402823840.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9a\u4e49json\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9a\u4e49json\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u5b9a\u4e49JSON\u6570\u7ec4\u7684\u957f\u5ea6<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49JSON\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528ArrayNode<\/strong><\/p>\n<p>ArrayNode\u662fJackson\u5e93\u4e2d\u8868\u793aJSON\u6570\u7ec4\u7684\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521b\u5efaArrayNode\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.fasterxml.jackson.databind.node.ArrayNode;\nimport com.fasterxml.jackson.databind.node.JsonNodeFactory;\n\nArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<pre>arrayNode.add(\"John\");\narrayNode.add(\"Mary\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528size()\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<pre>int length = arrayNode.size(); \/\/ 2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528JsonArray<\/strong><\/p>\n<p>JsonArray\u662fGson\u5e93\u4e2d\u8868\u793aJSON\u6570\u7ec4\u7684\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521b\u5efaJsonArray\uff1a<\/p>\n<pre>import com.google.gson.JsonArray;\n\nJsonArray jsonArray = new JsonArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<pre>jsonArray.add(\"John\");\njsonArray.add(\"Mary\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528size()\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<pre>int length = jsonArray.size(); \/\/ 2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9a\u4e49\u7684\u957f\u5ea6\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>\u4f7f\u7528jackson\u5e93\u7684arraynode\uff1a\u521b\u5efaarraynode\uff0c\u6dfb\u52a0\u5143\u7d20\u5e76\u8c03\u7528size()\u83b7\u53d6\u957f\u5ea6\u3002\u4f7f\u7528gson\u5e93\u7684jsonarray\uff1a\u521b\u5efajsonarray\uff0c\u6dfb\u52a0\u5143\u7d20\u5e76\u8c03\u7528size()\u83b7\u53d6\u957f\u5ea6\u3002 Java\u4e2d\u5b9a\u4e49JSON\u6570\u7ec4\u7684\u957f\u5ea6 \u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5b9a\u4e49JSON\u6570\u7ec4\u7684\u957f\u5ea6\uff1a 1. \u4f7f\u7528ArrayNode ArrayNode\u662fJackson\u5e93\u4e2d\u8868\u793aJSON\u6570\u7ec4\u7684\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521b\u5efaArrayNode\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode(); \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u53ef\u4ee5\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a arrayNode.add(&#8220;John&#8221;); arrayNode.add(&#8220;Mary&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528size()\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a int length = arrayNode.size(); \/\/ 2 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528JsonArray JsonArray\u662fGson\u5e93\u4e2d\u8868\u793aJSON\u6570\u7ec4\u7684\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521b\u5efaJsonArray\uff1a import com.google.gson.JsonArray; JsonArray jsonArray = new JsonArray(); \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u53ef\u4ee5\u4f7f\u7528add()\u65b9\u6cd5\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1a jsonArray.add(&#8220;John&#8221;); jsonArray.add(&#8220;Mary&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528size()\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a int length = jsonArray.size(); \/\/ 2 \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9a\u4e49\u7684\u957f\u5ea6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-66004","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66004","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=66004"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66004\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}