{"id":35465,"date":"2024-11-26T12:29:37","date_gmt":"2024-11-26T04:29:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35465\/"},"modified":"2024-11-26T12:29:37","modified_gmt":"2024-11-26T04:29:37","slug":"java%e6%80%8e%e4%b9%88%e5%8f%96json%e6%95%b0%e7%bb%84%e6%af%8f%e4%b8%aa%e5%b1%9e%e6%80%a7%e5%92%8c%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35465\/","title":{"rendered":"java\u600e\u4e48\u53d6json\u6570\u7ec4\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c"},"content":{"rendered":"<blockquote><p>\n  \u4ece java json \u6570\u7ec4\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 jackson \u5e93\uff1a\u4f7f\u7528 objectmapper \u89e3\u6790 json \u6570\u7ec4\u3002\u521b\u5efa jsonarray \u5bf9\u8c61\u5e76\u904d\u5386\uff0c\u83b7\u53d6\u952e\u503c\u5bf9\u3002\u4f7f\u7528 gson \u5e93\uff1a\u4f7f\u7528 gson \u89e3\u6790 json \u6570\u7ec4\u3002\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u83b7\u53d6\u952e\u503c\u5bf9\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110403425281054.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u53d6json\u6570\u7ec4\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\u63d2\u56fe\" alt=\"java\u600e\u4e48\u53d6json\u6570\u7ec4\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece Java JSON \u6570\u7ec4\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c<\/strong><\/p>\n<p>JSON \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5305\u542b\u4e00\u7cfb\u5217\u503c\u3002\u6bcf\u4e2a\u503c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u6216\u53e6\u4e00\u4e2a\u6570\u7ec4\u3002\u4e3a\u4e86\u4ece Java \u4e2d\u7684 JSON \u6570\u7ec4\u4e2d\u63d0\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>\u4f7f\u7528 Jackson \u5e93\u89e3\u6790 JSON \u6570\u7ec4<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\nimport com.fasterxml.jackson.core.JsonParser;\nimport org.json.JSONArray;  \/\/ \u5982\u679c\u4f7f\u7528 org.json \u5305\u4e2d\u7684 JSONArray\n\n\/\/ \u5047\u8bbe jsonStr \u662f\u5305\u542b JSON \u6570\u7ec4\u7684\u5b57\u7b26\u4e32\nString jsonStr = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Alice\", \"age\":25}]\";\n\n\/\/ \u521b\u5efa ObjectMapper \u5bf9\u8c61\nObjectMapper mapper = new ObjectMapper();\n\n\/\/ \u56e0\u4e3a JSON \u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u9700\u8981\u4f7f\u7528 JsonParser \u6765\u89e3\u6790\u5b83\nJsonParser parser = mapper.getFactory().createParser(jsonStr);\n\n\/\/ \u521b\u5efa\u4e00\u4e2a JSONArray \u5bf9\u8c61\nJSONArray jsonArray = null;\nif (parser.getCurrentToken() == JsonToken.START_ARRAY) {\n    jsonArray = new JSONArray(parser.readValueAsTree().toString());\n}\n\n\/\/ \u904d\u5386 JSON \u6570\u7ec4\nfor (int i = 0; i &lt; jsonArray.length(); i++) {\n    JSONObject jsonObject = jsonArray.getJSONObject(i);\n\n    \/\/ \u904d\u5386 JSON \u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\n    for (String key : jsonObject.keySet()) {\n        \/\/ \u83b7\u53d6\u952e\u548c\u503c\n        String value = jsonObject.getString(key);\n        System.out.println(\"Key: \" + key + \", Value: \" + value);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 Gson \u5e93\u89e3\u6790 JSON \u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import com.google.gson.Gson;\nimport com.google.gson.JsonArray;\nimport com.google.gson.JsonElement;\nimport com.google.gson.JsonObject;\n\n\/\/ \u5047\u8bbe jsonStr \u662f\u5305\u542b JSON \u6570\u7ec4\u7684\u5b57\u7b26\u4e32\nString jsonStr = \"[{\"name\":\"John\", \"age\":30}, {\"name\":\"Alice\", \"age\":25}]\";\n\n\/\/ \u521b\u5efa Gson \u5bf9\u8c61\nGson gson = new Gson();\n\n\/\/ \u89e3\u6790 JSON \u6570\u7ec4\nJsonArray jsonArray = gson.fromJson(jsonStr, JsonArray.class);\n\n\/\/ \u904d\u5386 JSON \u6570\u7ec4\nfor (JsonElement element : jsonArray) {\n    JsonObject jsonObject = element.getAsJsonObject();\n\n    \/\/ \u904d\u5386 JSON \u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\n    for (String key : jsonObject.keySet()) {\n        \/\/ \u83b7\u53d6\u952e\u548c\u503c\n        String value = jsonObject.get(key).getAsString();\n        System.out.println(\"Key: \" + key + \", Value: \" + value);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece Java \u4e2d\u7684 JSON \u6570\u7ec4\u4e2d\u63d0\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\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>\u4ece java json \u6570\u7ec4\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528 jackson \u5e93\uff1a\u4f7f\u7528 objectmapper \u89e3\u6790 json \u6570\u7ec4\u3002\u521b\u5efa jsonarray \u5bf9\u8c61\u5e76\u904d\u5386\uff0c\u83b7\u53d6\u952e\u503c\u5bf9\u3002\u4f7f\u7528 gson \u5e93\uff1a\u4f7f\u7528 gson \u89e3\u6790 json \u6570\u7ec4\u3002\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u83b7\u53d6\u952e\u503c\u5bf9\u3002 \u5982\u4f55\u4ece Java JSON \u6570\u7ec4\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c JSON \u6570\u7ec4\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u5305\u542b\u4e00\u7cfb\u5217\u503c\u3002\u6bcf\u4e2a\u503c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u503c\u3001\u5bf9\u8c61\u6216\u53e6\u4e00\u4e2a\u6570\u7ec4\u3002\u4e3a\u4e86\u4ece Java \u4e2d\u7684 JSON \u6570\u7ec4\u4e2d\u63d0\u53d6\u6bcf\u4e2a\u5c5e\u6027\u548c\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u4f7f\u7528 Jackson \u5e93\u89e3\u6790 JSON \u6570\u7ec4 import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonParser; import org.json.JSONArray; \/\/ \u5982\u679c\u4f7f\u7528 org.json \u5305\u4e2d\u7684 JSONArray \/\/ \u5047\u8bbe jsonStr \u662f\u5305\u542b JSON \u6570\u7ec4\u7684\u5b57\u7b26\u4e32 String jsonStr = &#8220;[{&#8220;name&#8221;:&#8221;John&#8221;, &#8220;age&#8221;:30}, [&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-35465","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35465","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=35465"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35465\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}