{"id":36943,"date":"2024-11-26T09:04:37","date_gmt":"2024-11-26T01:04:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36943\/"},"modified":"2024-11-26T09:04:37","modified_gmt":"2024-11-26T01:04:37","slug":"java-json%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%8f%96%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36943\/","title":{"rendered":"java json\u6570\u7ec4\u600e\u4e48\u53d6\u503c"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4ece json \u6570\u7ec4\u83b7\u53d6\u503c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 gson \u5e93\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a json \u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002\u4f7f\u7528 org.json \u5e93\uff1a\u521b\u5efa json \u6570\u7ec4\uff0c\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002\u4f7f\u7528 jackson \u5e93\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111222033075815.jpg\" class=\"aligncenter\" title=\"java json\u6570\u7ec4\u600e\u4e48\u53d6\u503c\u63d2\u56fe\" alt=\"java json\u6570\u7ec4\u600e\u4e48\u53d6\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d JSON \u6570\u7ec4\u53d6\u503c<\/strong><\/p>\n<p>Java \u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4ece JSON \u6570\u7ec4\u4e2d\u83b7\u53d6\u503c\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 GSON \u5e93<\/strong><\/p>\n<p>\u4f7f\u7528 GSON \u5e93\u662f\u83b7\u53d6 JSON \u6570\u7ec4\u503c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.google.gson.Gson;\nimport com.google.gson.JsonElement;\nimport com.google.gson.JsonArray;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[1, 2, 3]\";\n\n\/\/ \u521b\u5efa GSON \u5bf9\u8c61\nGson gson = new Gson();\n\n\/\/ \u89e3\u6790 JSON \u5b57\u7b26\u4e32\nJsonElement jsonElement = gson.fromJson(jsonString, JsonElement.class);\n\n\/\/ \u68c0\u67e5\u662f\u5426\u4e3a\u6570\u7ec4\nif (jsonElement.isJsonArray()) {\n\n    \/\/ \u8f6c\u6362\u4e3a JSON \u6570\u7ec4\n    JsonArray jsonArray = jsonElement.getAsJsonArray();\n\n    \/\/ \u904d\u5386\u6570\u7ec4\u5e76\u83b7\u53d6\u503c\n    for (JsonElement element : jsonArray) {\n        System.out.println(element.getAsInt()); \/\/ 1, 2, 3\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 org.json \u5e93<\/strong><\/p>\n<p>org.json \u5e93\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u83b7\u53d6 JSON \u6570\u7ec4\u503c\u7684\u65b9\u6cd5\u3002<\/p>\n<pre>import org.json.JSONArray;\nimport org.json.JSONObject;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[1, 2, 3]\";\n\n\/\/ \u521b\u5efa JSON \u6570\u7ec4\nJSONArray jsonArray = new JSONArray(jsonString);\n\n\/\/ \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\nint length = jsonArray.length();\n\n\/\/ \u904d\u5386\u6570\u7ec4\u5e76\u83b7\u53d6\u503c\nfor (int i = 0; i &lt; length; i++) {\n    System.out.println(jsonArray.getInt(i)); \/\/ 1, 2, 3\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Jackson \u5e93<\/strong><\/p>\n<p>Jackson \u5e93\u4e5f\u5141\u8bb8\u4ece JSON \u6570\u7ec4\u4e2d\u83b7\u53d6\u503c\u3002<\/p>\n<pre>import com.fasterxml.jackson.core.JsonParseException;\nimport com.fasterxml.jackson.databind.JsonMappingException;\nimport com.fasterxml.jackson.databind.ObjectMapper;\n\n\/\/ JSON \u5b57\u7b26\u4e32\nString jsonString = \"[1, 2, 3]\";\n\n\/\/ \u521b\u5efa ObjectMapper \u5bf9\u8c61\nObjectMapper mapper = new ObjectMapper();\n\n\/\/ \u8f6c\u6362\u4e3a\u6570\u7ec4\nint[] array = mapper.readValue(jsonString, int[].class);\n\n\/\/ \u904d\u5386\u6570\u7ec4\u5e76\u83b7\u53d6\u503c\nfor (int element : array) {\n    System.out.println(element); \/\/ 1, 2, 3\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u600e\u4e48\u53d6\u503c\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>java \u4e2d\u4ece json \u6570\u7ec4\u83b7\u53d6\u503c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528 gson \u5e93\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a json \u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002\u4f7f\u7528 org.json \u5e93\uff1a\u521b\u5efa json \u6570\u7ec4\uff0c\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002\u4f7f\u7528 jackson \u5e93\uff1a\u5c06 json \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u6570\u7ec4\uff0c\u7136\u540e\u904d\u5386\u6570\u7ec4\u83b7\u53d6\u503c\u3002 Java \u4e2d JSON \u6570\u7ec4\u53d6\u503c Java \u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4ece JSON \u6570\u7ec4\u4e2d\u83b7\u53d6\u503c\uff1a 1. \u4f7f\u7528 GSON \u5e93 \u4f7f\u7528 GSON \u5e93\u662f\u83b7\u53d6 JSON \u6570\u7ec4\u503c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonArray; \/\/ JSON \u5b57\u7b26\u4e32 String jsonString = &#8220;[1, 2, 3]&#8221;; \/\/ \u521b\u5efa GSON \u5bf9\u8c61 [&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-36943","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36943","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=36943"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36943\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}