{"id":40285,"date":"2024-11-26T16:23:00","date_gmt":"2024-11-26T08:23:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40285\/"},"modified":"2024-11-26T16:23:00","modified_gmt":"2024-11-26T08:23:00","slug":"java%e7%9a%84json%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e8%bd%ac%e6%8d%a2%e6%88%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40285\/","title":{"rendered":"java\u7684json\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u6b65\u9aa4\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 jackson \u6216 gson \u7b49 json \u89e3\u6790\u5e93\u89e3\u6790 json \u5b57\u7b26\u4e32\u3002\u4ece\u6839\u8282\u70b9\u83b7\u53d6\u8868\u793a\u6570\u7ec4\u7684 json \u8282\u70b9\u3002\u904d\u5386\u6570\u7ec4\u8282\u70b9\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u3002\u4ece\u5143\u7d20\u8282\u70b9\u4e2d\u83b7\u53d6\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111816091855649.jpg\" class=\"aligncenter\" title=\"java\u7684json\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u7684json\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6362\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u89e3\u6790 JSON \u5b57\u7b26\u4e32<\/strong><\/p>\n<p>\u4f7f\u7528 JSON \u89e3\u6790\u5e93\uff08\u5982 Jackson \u6216 Gson\uff09\u6765\u89e3\u6790 JSON \u5b57\u7b26\u4e32\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528 Jackson \u5e93\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\nObjectMapper mapper = new ObjectMapper();\nJsonNode rootNode = mapper.readTree(jsonString);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u83b7\u53d6\u6570\u7ec4\u8282\u70b9<\/strong><\/p>\n<p>\u4ece\u6839\u8282\u70b9\u4e2d\u83b7\u53d6\u8868\u793a\u6570\u7ec4\u7684 JSON \u8282\u70b9\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c JSON \u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u7ec4\u540d\u4e3a &#8220;myArray&#8221;\uff1a<\/p>\n<pre>JsonNode arrayNode = rootNode.path(\"myArray\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u904d\u5386\u6570\u7ec4\u8282\u70b9<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u904d\u5386\u6570\u7ec4\u8282\u70b9\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre>for (JsonNode elementNode : arrayNode) {\n  \/\/ \u5904\u7406\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4ece\u5143\u7d20\u8282\u70b9\u4e2d\u83b7\u53d6\u503c<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\u8282\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u5176\u503c\uff1a<\/p>\n<pre>String value = elementNode.asText();\n\/\/ \u6216\u8005\nint value = elementNode.asInt();\n\/\/ \u7b49\u7b49<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Java \u4e2d\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u7684json\u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6362\u6210\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>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u6b65\u9aa4\u5c06 json \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 jackson \u6216 gson \u7b49 json \u89e3\u6790\u5e93\u89e3\u6790 json \u5b57\u7b26\u4e32\u3002\u4ece\u6839\u8282\u70b9\u83b7\u53d6\u8868\u793a\u6570\u7ec4\u7684 json \u8282\u70b9\u3002\u904d\u5386\u6570\u7ec4\u8282\u70b9\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u3002\u4ece\u5143\u7d20\u8282\u70b9\u4e2d\u83b7\u53d6\u503c\u3002 \u5982\u4f55\u5c06 Java \u4e2d\u7684 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a 1. \u89e3\u6790 JSON \u5b57\u7b26\u4e32 \u4f7f\u7528 JSON \u89e3\u6790\u5e93\uff08\u5982 Jackson \u6216 Gson\uff09\u6765\u89e3\u6790 JSON \u5b57\u7b26\u4e32\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f8b\u5982\uff0c\u4f7f\u7528 Jackson \u5e93\u8fdb\u884c\u89e3\u6790\uff1a import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readTree(jsonString); \u767b\u5f55\u540e\u590d\u5236 2. \u83b7\u53d6\u6570\u7ec4\u8282\u70b9 [&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-40285","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40285","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=40285"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40285\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}