{"id":2205,"date":"2022-07-10T22:50:18","date_gmt":"2022-07-10T14:50:18","guid":{"rendered":"https:\/\/coderbee.net\/?p=2205"},"modified":"2022-07-10T22:50:18","modified_gmt":"2022-07-10T14:50:18","slug":"%e5%b0%8f%e5%bf%83-fastjson-%e7%9a%84%e8%bf%99%e7%a7%8d%e6%99%ba%e8%83%bd","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20220710\/2205","title":{"rendered":"\u5c0f\u5fc3 fastjson \u7684\u8fd9\u79cd\u201c\u667a\u80fd\u201d"},"content":{"rendered":"<p>\u6700\u8fd1\u78b0\u5230\u4e00\u4e2a\u73b0\u8c61\u6216\u8005\u8bf4\u95ee\u9898\uff0c\u540c\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cSpring \u9ed8\u8ba4\u7684 Jackson \u7c7b\u5e93\u89e3\u6790\u62a5\u9519\uff0cfastjson \u5374\u6ca1\u62a5\u9519\u3001\u6b63\u5e38\u89e3\u6790\u4e86\u3002<\/p>\n<p>\u573a\u666f\u5927\u6982\u662f\u8fd9\u6837\u7684\uff0c\u6709\u4e2a\u7c7b\u6709\u4e2a\u65e5\u671f\u5c5e\u6027\uff0c\u683c\u5f0f\u6307\u5b9a\u4e3a &#8220;yyyy-MM-dd&#8221;\u3002<\/p>\n<pre><code class=\"java\">@Data\nstatic class Person {\n    @JsonFormat(pattern = \"yyyy-MM-dd\", timezone = \"GMT+8\") \/\/ Jackson\n    @JSONField(format = \"yyyy-MM-dd\") \/\/ fastjson\n    Date birthDay;\n\n    @JsonFormat(pattern = \"yyyy-MM-dd\", timezone = \"GMT+8\")\n    @JSONField(format = \"yyyy-MM-dd\")\n    Date today;\n\n    String name;\n}\n<\/code><\/pre>\n<p>\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code class=\"java\">public void testFastJson() throws JsonProcessingException {\n    String json = \"{\\\"birthDay\\\":\\\"2022710\\\", \\\"name\\\": \\\"coderbee\\\", \\\"today\\\":\\\"2022-07-10\\\"}\";\n    Person person = JSONObject.parseObject(json, Person.class);\n    System.out.println(person);     \/\/ \u8f93\u51fa\u89e3\u6790\u5230\u5bf9\u8c61\n    System.out.println(JSONObject.toJSONString(person)); \/\/ \u628a\u5bf9\u8c61\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u518d\u8f93\u51fa\u3002\n\n    ObjectMapper mapper = new ObjectMapper();\n    Person jacksonPerson = mapper.readValue(json, Person.class);\n    System.out.println(jacksonPerson);\n}\n<\/code><\/pre>\n<p><!--more--><\/p>\n<p>\u5f97\u5230\u7684\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>Person(birthDay=Sun Jul 10 00:00:00 CST 2022, name=coderbee)\n{\"birthDay\":\"2022-07-10\",\"name\":\"coderbee\"}\n\ncom.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String \"20220710\": expected format \"yyyy-MM-dd\"\n at [Source: (String)\"{\"birthDay\":\"20220710\", \"name\": \"coderbee\"}\"; line: 1, column: 13] (through reference chain: net.coderbee.algorithm.SnowFlakeTest$Person[\"birthDay\"])\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230 fastjson \u89e3\u6790\u6ca1\u6709\u62a5\u9519\uff0c\u201c\u667a\u80fd\u201d\u5730\u5904\u7406\u4e86\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u95ee\u9898\uff0c\u800c jackson \u62a5\u9519\u4e86\u3002<br \/>\n\u6211\u4eec\u518d\u770b\u770b\u4e0a\u9762\u8f93\u51fa\u7684\u7b2c\u4e8c\u884c\uff0c\u5c31\u662f\u628a fastjson \u89e3\u6790\u5230\u7684\u5bf9\u8c61\u518d\u6b21\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u770b\u5230 birthDay \u7684\u683c\u5f0f\u662f\u7b26\u5408\u4ee3\u7801\u5b9a\u4e49\u7684\uff0c\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u4e0e\u8f93\u5165\u662f\u4e0d\u4e00\u81f4\u7684\u3002<\/p>\n<p>\u5f53\u6211\u628a birthDay \u7684\u5b57\u7b26\u4e32\u66ff\u6362\u4e3a &#8220;2022710&#8221;\uff0c\u4ecd\u7136\u6ca1\u6709\u62a5\u9519\uff0c\u4f46\u89e3\u6790\u5230\u7684\u7ed3\u679c\u662f\u9519\u8bef\u7684\u4e86\uff1a<\/p>\n<pre><code>Person(birthDay=Thu Jan 01 08:33:42 CST 1970, name=coderbee)\n{\"birthDay\":\"1970-01-01\",\"name\":\"coderbee\"}\n<\/code><\/pre>\n<p>\u4e0d\u8d5e\u6210\u8fd9\u79cd\u201c\u667a\u80fd\u201d\uff0c\u8be5\u62a5\u9519\u7684\u65f6\u5019\u5c31\u5f97\u62a5\u9519\uff0c\u8fd9\u6837\u624d\u77e5\u9053\u4f20\u9519\u4e86\u3002\u6700\u4e3b\u8981\u7684\u662f\uff0c\u8981\u5207\u6362\u5176\u4ed6 JSON \u5b9e\u73b0\u5e93\u624d\u4e0d\u4f1a\u7a81\u7136\u62a5\u9519\u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u78b0\u5230\u4e00\u4e2a\u73b0\u8c61\u6216\u8005\u8bf4\u95ee\u9898\uff0c\u540c\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cSpring \u9ed8\u8ba4\u7684 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20220710\/2205\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[359,231],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2205"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2205"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2205\/revisions"}],"predecessor-version":[{"id":2206,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2205\/revisions\/2206"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}