{"id":38771,"date":"2024-11-26T09:32:05","date_gmt":"2024-11-26T01:32:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38771\/"},"modified":"2024-11-26T09:32:05","modified_gmt":"2024-11-26T01:32:05","slug":"json%e6%80%8e%e4%b9%88%e8%bd%ac%e5%8c%96%e6%88%90%e6%95%b0%e7%bb%84-java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38771\/","title":{"rendered":"json\u600e\u4e48\u8f6c\u5316\u6210\u6570\u7ec4 java"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528 gson\u3001jackson \u6216 org.json \u5e93\u5c06 json \u8f6c\u6362\u4e3a java \u6570\u7ec4\u3002\u4f7f\u7528 gson\uff1a1. \u5bfc\u5165 gson \u5e93\uff1b2. \u4f7f\u7528 typetoken \u6307\u5b9a\u6570\u7ec4\u7c7b\u578b\uff1b3. \u4f7f\u7528 gson.fromjson \u8f6c\u6362 json\u3002\u4f7f\u7528 jackson\uff1a1. \u5bfc\u5165 jackson \u5e93\uff1b2. \u521b\u5efa objectmapper\uff1b3. \u4f7f\u7528 readvalue \u8f6c\u6362 json\u3002\u4f7f\u7528 org.json\uff1a1. \u5bfc\u5165 org.json \u5e93\uff1b2. \u521b\u5efa jsonarray\uff1b3. \u904d\u5386 jsonarra\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111506124959418.jpg\" class=\"aligncenter\" title=\"json\u600e\u4e48\u8f6c\u5316\u6210\u6570\u7ec4 java\u63d2\u56fe\" alt=\"json\u600e\u4e48\u8f6c\u5316\u6210\u6570\u7ec4 java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 JSON \u8f6c\u6362\u4e3a Java \u6570\u7ec4<\/strong><\/p>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u4e2d\u3002Java \u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06 JSON \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><strong>\u4f7f\u7528 Gson \u5e93<\/strong><\/p>\n<p>Gson \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Java \u5e93\uff0c\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u5c06 JSON \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.google.gson.Gson;\nimport com.google.gson.reflect.TypeToken;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString json = \"[1, 2, 3, 4, 5]\";\n\n\/\/ \u4f7f\u7528 Gson \u8f6c\u6362\u4e3a\u6570\u7ec4\nTypeToken&lt;int[]&gt; typeToken = new TypeToken&lt;int[]&gt;() {};\nint[] array = new Gson().fromJson(json, typeToken.getType());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Jackson \u5e93<\/strong><\/p>\n<p>Jackson \u4e5f\u662f\u4e00\u4e2a\u5e38\u7528\u7684 Java \u5e93\uff0c\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06 JSON \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString json = \"[1, 2, 3, 4, 5]\";\n\n\/\/ \u4f7f\u7528 Jackson \u8f6c\u6362\u4e3a\u6570\u7ec4\nObjectMapper mapper = new ObjectMapper();\nint[] array = mapper.readValue(json, int[].class);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 org.json \u5e93<\/strong><\/p>\n<p>org.json \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Java \u5e93\uff0c\u7528\u4e8e\u5904\u7406 JSON \u6570\u636e\u3002\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06 JSON \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>import org.json.JSONArray;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString json = \"[1, 2, 3, 4, 5]\";\n\n\/\/ \u4f7f\u7528 org.json \u8f6c\u6362\u4e3a\u6570\u7ec4\nJSONArray jsonArray = new JSONArray(json);\nint[] array = new int[jsonArray.length()];\nfor (int i = 0; i &lt; jsonArray.length(); i++) {\n    array[i] = jsonArray.getInt(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong><\/p>\n<p>Gson\u3001Jackson \u548c org.json \u5e93\u90fd\u662f\u8f6c\u6362 JSON \u7684\u4e0d\u9519\u9009\u62e9\u3002\u9009\u62e9\u54ea\u79cd\u5e93\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u548c\u504f\u597d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjson\u600e\u4e48\u8f6c\u5316\u6210\u6570\u7ec4 java\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\u7528 gson\u3001jackson \u6216 org.json \u5e93\u5c06 json \u8f6c\u6362\u4e3a java \u6570\u7ec4\u3002\u4f7f\u7528 gson\uff1a1. \u5bfc\u5165 gson \u5e93\uff1b2. \u4f7f\u7528 typetoken \u6307\u5b9a\u6570\u7ec4\u7c7b\u578b\uff1b3. \u4f7f\u7528 gson.fromjson \u8f6c\u6362 json\u3002\u4f7f\u7528 jackson\uff1a1. \u5bfc\u5165 jackson \u5e93\uff1b2. \u521b\u5efa objectmapper\uff1b3. \u4f7f\u7528 readvalue \u8f6c\u6362 json\u3002\u4f7f\u7528 org.json\uff1a1. \u5bfc\u5165 org.json \u5e93\uff1b2. \u521b\u5efa jsonarray\uff1b3. \u904d\u5386 jsonarra \u5982\u4f55\u5c06 JSON \u8f6c\u6362\u4e3a Java \u6570\u7ec4 JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e7f\u6cdb\u7528\u4e8e Web \u5f00\u53d1\u4e2d\u3002Java \u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06 JSON \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 \u4f7f\u7528 Gson \u5e93 Gson [&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-38771","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38771","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=38771"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38771\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}