{"id":97,"date":"2023-02-18T06:31:17","date_gmt":"2023-02-17T22:31:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=97"},"modified":"2023-02-18T06:37:16","modified_gmt":"2023-02-17T22:37:16","slug":"fastjson-null-property","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/fastjson-null-property\/","title":{"rendered":"fastjson\u751f\u6210json\u65f6Null\u5c5e\u6027\u4e0d\u663e\u793a"},"content":{"rendered":"<p>\u5728\u4f7f\u7528Fastjson\u89e3\u6790json\u65f6\u53d1\u73b0\uff0c\u9ed8\u8ba4\u5904\u7406null\u65f6\uff0c\u65e0\u8bba\u662fMap\u8fd8\u662fObject\uff0c\u7ed3\u679c\u4f1a\u628anull\u7684\u5b57\u6bb5\u4ee5\u53ca\u503c\u8fc7\u6ee4\u6389\uff0c\u4e1a\u52a1\u4e0a\u9700\u8981\u4fdd\u7559null\u503c<\/p>\n<pre><code class=\"language-java\">Map&lt;String, Object&gt; jsonMap = new HashMap&lt;String, Object&gt;();\njsonMap.put(&quot;a&quot;, 1);\njsonMap.put(&quot;b&quot;, &quot;&quot;);\njsonMap.put(&quot;c&quot;, null);\njsonMap.put(&quot;d&quot;, &quot;appblog.cn&quot;);\n\nString str = JSONObject.toJSONString(jsonMap);\nSystem.out.println(str);\n\/\/\u8f93\u51fa\u7ed3\u679c:{&quot;a&quot;:1,&quot;b&quot;:&quot;&quot;,d:&quot;appblog.cn&quot;}<\/code><\/pre>\n<p>\u4ece\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cnull\u5bf9\u5e94\u7684key\u5df2\u7ecf\u88ab\u8fc7\u6ee4\u6389\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Fastjson\u7684SerializerFeature\u5e8f\u5217\u5316\u5c5e\u6027<\/p>\n<pre><code class=\"language-java\">JSONObject.toJSONString(Object object, SerializerFeature... features)<\/code><\/pre>\n<p>Fastjson\u7684<code>SerializerFeature<\/code>\u5e8f\u5217\u5316\u5c5e\u6027<\/p>\n<ul>\n<li><code>QuoteFieldNames<\/code>: \u8f93\u51fakey\u65f6\u662f\u5426\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u9ed8\u8ba4\u4e3atrue<\/li>\n<li><code>WriteMapNullValue<\/code>: \u662f\u5426\u8f93\u51fa\u503c\u4e3anull\u7684\u5b57\u6bb5\uff0c\u9ed8\u8ba4\u4e3afalse<\/li>\n<li><code>WriteNullNumberAsZero<\/code>: \u6570\u503c\u5b57\u6bb5\u5982\u679c\u4e3anull\uff0c\u8f93\u51fa\u4e3a0\uff0c\u800c\u975enull<\/li>\n<li><code>WriteNullListAsEmpty<\/code>: List\u5b57\u6bb5\u5982\u679c\u4e3anull\uff0c\u8f93\u51fa\u4e3a[]\uff0c\u800c\u975enull<\/li>\n<li><code>WriteNullStringAsEmpty<\/code>: \u5b57\u7b26\u7c7b\u578b\u5b57\u6bb5\u5982\u679c\u4e3anull\uff0c\u8f93\u51fa\u4e3a&quot;&quot;\uff0c\u800c\u975enull<\/li>\n<li><code>WriteNullBooleanAsFalse<\/code>: Boolean\u5b57\u6bb5\u5982\u679c\u4e3anull\uff0c\u8f93\u51fa\u4e3afalse\uff0c\u800c\u975enull<\/li>\n<\/ul>\n<pre><code class=\"language-java\">Map&lt;String, Object&gt; jsonMap = new HashMap&lt;String, Object&gt;();\njsonMap.put(&quot;a&quot;, 1);\njsonMap.put(&quot;b&quot;, &quot;&quot;);\njsonMap.put(&quot;c&quot;, null);\njsonMap.put(&quot;d&quot;, &quot;appblog.cn&quot;);\n\nString str = JSONObject.toJSONString(jsonMap, SerializerFeature.WriteMapNullValue);\nSystem.out.println(str);\n\/\/\u8f93\u51fa\u7ed3\u679c:{&quot;a&quot;:1,&quot;b&quot;:&quot;&quot;,&quot;c&quot;:null,d:&quot;appblog.cn&quot;}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528Fastjson\u89e3\u6790json\u65f6\u53d1\u73b0\uff0c\u9ed8\u8ba4\u5904\u7406null\u65f6\uff0c\u65e0\u8bba\u662fMap\u8fd8\u662fObject\uff0c\u7ed3\u679c\u4f1a\u628anull\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[58],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-fastjson"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}