{"id":111,"date":"2026-01-30T09:20:59","date_gmt":"2026-01-30T01:20:59","guid":{"rendered":"https:\/\/wp.buildyou.top\/?p=111"},"modified":"2026-02-04T17:14:17","modified_gmt":"2026-02-04T09:14:17","slug":"java%e5%88%a4%e6%96%adjson%e6%98%af%e5%90%a6%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/wp.buildyou.top\/index.php\/2026\/01\/30\/java%e5%88%a4%e6%96%adjson%e6%98%af%e5%90%a6%e7%a9%ba\/","title":{"rendered":"java\u5224\u65adjson\u662f\u5426\u7a7a"},"content":{"rendered":"\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary><\/summary><\/details>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Java \u4e2d\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u201c\u7a7a JSON\u201d\uff0c\u901a\u5e38\u9700\u8981\u533a\u5206\u4e24\u79cd\u60c5\u51b5\uff1a<strong>\u4e00\u79cd\u662f\u5b57\u7b26\u4e32\u672c\u8eab\u4e3a\u7a7a\uff08null \u6216 &#8220;&#8221;\uff09<\/strong>\uff0c\u53e6\u4e00\u79cd\u662f<strong>\u7b26\u5408 JSON \u683c\u5f0f\u4f46\u6ca1\u6709\u5185\u5bb9\uff08<code>{}<\/code> \u6216 <code>[]<\/code>\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u4f60\u4f7f\u7528\u7684 JSON \u7c7b\u5e93\uff08Jackson, Fastjson, \u6216 Gson\uff09\uff0c\u5904\u7406\u65b9\u5f0f\u7565\u6709\u4e0d\u540c\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 Jackson (\u63a8\u8350)<\/h3>\n\n\n\n<p>Jackson \u662f Spring Boot \u9ed8\u8ba4\u7684 JSON \u5e93\uff0c\u529f\u80fd\u6700\u7a33\u5065\u3002<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;import com.fasterxml.jackson.databind.JsonNode;<br>&nbsp;import com.fasterxml.jackson.databind.ObjectMapper;<br>&nbsp;\u200b<br>&nbsp;public boolean isJsonEmpty(String jsonStr) {<br>&nbsp; &nbsp;  if (jsonStr == null || jsonStr.trim().isEmpty()) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return true;<br>&nbsp; &nbsp;  }<br>&nbsp; &nbsp;  try {<br>&nbsp; &nbsp; &nbsp; &nbsp;  ObjectMapper mapper = new ObjectMapper();<br>&nbsp; &nbsp; &nbsp; &nbsp;  JsonNode node = mapper.readTree(jsonStr);<br>&nbsp; &nbsp; &nbsp; &nbsp;  \/\/ isEmpty() \u53ef\u4ee5\u68c0\u67e5 {} \u6216 []<br>&nbsp; &nbsp; &nbsp; &nbsp;  return node.isEmpty();<br>&nbsp; &nbsp;  } catch (Exception e) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return true; \/\/ \u683c\u5f0f\u975e\u6cd5\u4e5f\u53ef\u89c6\u4e3a\u5e7f\u4e49\u4e0a\u7684\u7a7a<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 Fastjson (\u5e38\u89c1\u4e8e\u56fd\u5185\u9879\u76ee)<\/h3>\n\n\n\n<p>Fastjson \u63d0\u4f9b\u4e86\u975e\u5e38\u76f4\u63a5\u7684\u5224\u65ad\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;import com.alibaba.fastjson.JSON;<br>&nbsp;import com.alibaba.fastjson.JSONObject;<br>&nbsp;\u200b<br>&nbsp;public boolean isJsonEmpty(String jsonStr) {<br>&nbsp; &nbsp;  if (jsonStr == null || jsonStr.isEmpty()) return true;<br>&nbsp; &nbsp;  try {<br>&nbsp; &nbsp; &nbsp; &nbsp;  Object obj = JSON.parse(jsonStr);<br>&nbsp; &nbsp; &nbsp; &nbsp;  if (obj instanceof JSONObject) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return ((JSONObject) obj).isEmpty();<br>&nbsp; &nbsp; &nbsp; &nbsp;  } else if (obj instanceof JSONArray) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return ((JSONArray) obj).isEmpty();<br>&nbsp; &nbsp; &nbsp; &nbsp;  }<br>&nbsp; &nbsp; &nbsp; &nbsp;  return false;<br>&nbsp; &nbsp;  } catch (Exception e) {<br>&nbsp; &nbsp; &nbsp; &nbsp;  return true;<br>&nbsp; &nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7b80\u5355\u7684\u6b63\u5219\/\u5b57\u7b26\u4e32\u5224\u65ad (\u8f7b\u91cf\u7ea7)<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u89e3\u6790\u6574\u4e2a\u5bf9\u8c61\uff08\u4e3a\u4e86\u6027\u80fd\uff09\uff0c\u53ea\u60f3\u5feb\u901f\u5224\u65ad\u662f\u4e0d\u662f <code>{}<\/code> \u6216 <code>[]<\/code>\uff1a<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;public static boolean isBasicEmptyJson(String str) {<br>&nbsp; &nbsp;  if (str == null) return true;<br>&nbsp; &nbsp;  String trimmed = str.trim();<br>&nbsp; &nbsp;  return trimmed.equals(\"{}\") || trimmed.equals(\"[]\") || trimmed.isEmpty();<br>&nbsp;}<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\u5bf9\u6bd4<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350\u65b9\u6848<\/th><th>\u7406\u7531<\/th><\/tr><\/thead><tbody><tr><td>\u4e25\u8c28\u6821\u9a8c<\/td><td>Jacksonnode.isEmpty()<\/td><td>\u80fd\u8bc6\u522b{ }(\u5e26\u7a7a\u683c) \u7b49\u590d\u6742\u60c5\u51b5\u3002<\/td><\/tr><tr><td>\u6781\u81f4\u6027\u80fd<\/td><td>\u5b57\u7b26\u4e32equals(&#8220;{}&#8221;)<\/td><td>\u65e0\u9700\u521b\u5efa\u89e3\u6790\u5bf9\u8c61\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u683c\u5f0f\u7b80\u5355\u7684\u573a\u666f\u3002<\/td><\/tr><tr><td>\u901a\u7528\u4e1a\u52a1<\/td><td>Fastjson<\/td><td>\u4ee3\u7801\u91cf\u5c11\uff0cAPI \u76f4\u89c2\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u201c\u7a7a JSON\u201d\uff0c\u901a\u5e38\u9700\u8981\u533a\u5206\u4e24\u79cd\u60c5\u51b5\uff1a\u4e00\u79cd\u662f\u5b57\u7b26\u4e32\u672c\u8eab\u4e3a\u7a7a\uff08null \u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[24],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-20","tag-json"],"_links":{"self":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":2,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/posts\/111\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.buildyou.top\/index.php\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}