{"id":65776,"date":"2025-05-03T10:02:10","date_gmt":"2025-05-03T02:02:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65776\/"},"modified":"2025-05-03T10:02:10","modified_gmt":"2025-05-03T02:02:10","slug":"java%e6%80%8e%e4%b9%88%e5%b0%81%e8%a3%85json%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65776\/","title":{"rendered":"java\u600e\u4e48\u5c01\u88c5json\u5bf9\u8c61\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u5c01\u88c5 json \u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u7528\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 objectmapper \u548c jackson \u5e93\uff1a\u521b\u5efa objectmapper \u5e76\u521b\u5efa json \u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528 writevalueasstring \u65b9\u6cd5\u3002\u4f7f\u7528 gson \u5e93\uff1a\u521b\u5efa gson \u5bf9\u8c61\u548c json \u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528 tojson \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309573248045.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5c01\u88c5json\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5c01\u88c5json\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c01\u88c5 Java \u4e2d\u7684 JSON \u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5c01\u88c5 JSON \u5bf9\u8c61\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u672c\u6587\u5c06\u63d0\u4f9b\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 ObjectMapper \u548c Jackson \u5e93\uff0c\u4ee5\u53ca\u4f7f\u7528 Gson \u5e93\u3002<\/p>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 ObjectMapper \u548c Jackson \u5e93<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa ObjectMapper \u5bf9\u8c61<\/strong><\/p>\n<pre>import com.fasterxml.jackson.databind.ObjectMapper;\nObjectMapper objectMapper = new ObjectMapper();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u521b\u5efa JSON \u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<pre>JSONArray array = new JSONArray();\nJSONObject obj1 = new JSONObject();\nobj1.put(\"name\", \"John\");\nobj1.put(\"age\", 25);\nJSONObject obj2 = new JSONObject();\nobj2.put(\"name\", \"Mary\");\nobj2.put(\"age\", 30);\narray.add(obj1);\narray.add(obj2);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5c06 JSON \u5bf9\u8c61\u6570\u7ec4\u5c01\u88c5\u4e3a\u5b57\u7b26\u4e32<\/strong><\/p>\n<pre>String jsonString = objectMapper.writeValueAsString(array);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528 Gson \u5e93<\/strong><\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa Gson \u5bf9\u8c61<\/strong><\/p>\n<pre>import com.google.gson.Gson;\nGson gson = new Gson();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 2\uff1a\u521b\u5efa JSON \u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<pre>Object[] array = new Object[] {\n    new JsonObject().addProperty(\"name\", \"John\").addProperty(\"age\", 25),\n    new JsonObject().addProperty(\"name\", \"Mary\").addProperty(\"age\", 30)\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4 3\uff1a\u5c06 JSON \u5bf9\u8c61\u6570\u7ec4\u5c01\u88c5\u4e3a\u5b57\u7b26\u4e32<\/strong><\/p>\n<pre>String jsonString = gson.toJson(array);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5c01\u88c5json\u5bf9\u8c61\u6570\u7ec4\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>\u5728 java \u4e2d\u5c01\u88c5 json \u5bf9\u8c61\u6570\u7ec4\u53ef\u4ee5\u7528\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 objectmapper \u548c jackson \u5e93\uff1a\u521b\u5efa objectmapper \u5e76\u521b\u5efa json \u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528 writevalueasstring \u65b9\u6cd5\u3002\u4f7f\u7528 gson \u5e93\uff1a\u521b\u5efa gson \u5bf9\u8c61\u548c json \u5bf9\u8c61\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528 tojson \u65b9\u6cd5\u3002 \u5982\u4f55\u5c01\u88c5 Java \u4e2d\u7684 JSON \u5bf9\u8c61\u6570\u7ec4 \u5f15\u8a00 \u5728 Java \u4e2d\uff0c\u5c01\u88c5 JSON \u5bf9\u8c61\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u672c\u6587\u5c06\u63d0\u4f9b\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 ObjectMapper \u548c Jackson \u5e93\uff0c\u4ee5\u53ca\u4f7f\u7528 Gson \u5e93\u3002 \u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 ObjectMapper \u548c Jackson \u5e93 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6b65\u9aa4 1\uff1a\u521b\u5efa ObjectMapper \u5bf9\u8c61 import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper objectMapper = [&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-65776","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65776","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=65776"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65776\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}