{"id":65867,"date":"2025-05-03T09:52:21","date_gmt":"2025-05-03T01:52:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65867\/"},"modified":"2025-05-03T09:52:21","modified_gmt":"2025-05-03T01:52:21","slug":"java%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%adjson%e6%95%b0%e7%bb%84%e9%95%bf%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65867\/","title":{"rendered":"java\u600e\u4e48\u5224\u65adjson\u6570\u7ec4\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5224\u65ad java \u4e2d json \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a1. \u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a json \u6570\u7ec42. \u83b7\u53d6\u6570\u7ec4\u7684 length()\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313273733281.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5224\u65adjson\u6570\u7ec4\u957f\u5ea6\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5224\u65adjson\u6570\u7ec4\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad Java \u4e2d JSON \u6570\u7ec4\u7684\u957f\u5ea6<\/strong><\/p>\n<p>\u8981\u5224\u65ad Java \u4e2d JSON \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u5c06 JSON \u4e3a JSON \u6570\u7ec4<\/strong><\/p>\n<pre>JSONArray jsonArray = new JSONArray(jsonString);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int length = jsonArray.length();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>jsonString \u662f\u8981\u5904\u7406\u7684 JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li>jsonArray \u662f\u89e3\u6790\u540e\u7684 JSON \u6570\u7ec4\u3002<\/li>\n<li>length \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>String jsonString = \"[1, 2, 3, 4, 5]\";\nJSONArray jsonArray = new JSONArray(jsonString);\nint length = jsonArray.length();\nSystem.out.println(\"JSON \u6570\u7ec4\u7684\u957f\u5ea6\u4e3a\uff1a\" + length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>JSON \u6570\u7ec4\u7684\u957f\u5ea6\u4e3a\uff1a5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5224\u65ad\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>\u8981\u5224\u65ad java \u4e2d json \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a1. \u89e3\u6790 json \u5b57\u7b26\u4e32\u4e3a json \u6570\u7ec42. \u83b7\u53d6\u6570\u7ec4\u7684 length() \u5982\u4f55\u5224\u65ad Java \u4e2d JSON \u6570\u7ec4\u7684\u957f\u5ea6 \u8981\u5224\u65ad Java \u4e2d JSON \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u5c06 JSON \u4e3a JSON \u6570\u7ec4 JSONArray jsonArray = new JSONArray(jsonString); \u767b\u5f55\u540e\u590d\u5236 2. \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int length = jsonArray.length(); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a jsonString \u662f\u8981\u5904\u7406\u7684 JSON \u5b57\u7b26\u4e32\u3002 jsonArray \u662f\u89e3\u6790\u540e\u7684 JSON \u6570\u7ec4\u3002 length \u662f\u6570\u7ec4\u7684\u957f\u5ea6\u3002 \u793a\u4f8b\uff1a String [&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-65867","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65867","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=65867"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65867\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}