{"id":694,"date":"2023-02-26T14:58:17","date_gmt":"2023-02-26T06:58:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=694"},"modified":"2023-02-26T14:58:37","modified_gmt":"2023-02-26T06:58:37","slug":"fastjson-jsonfield-annotation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/fastjson-jsonfield-annotation\/","title":{"rendered":"FastJson\u4e2d@JSONField\u6ce8\u89e3\u4f7f\u7528"},"content":{"rendered":"<p>\u7531\u4e8eJson\u4e2d\u7684key\u4e0ebean\u4e2d\u7684\u5c5e\u6027\u4e0d\u80fd\u5339\u914d\uff0c\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u90e8\u5206\u5c5e\u6027\u4e3anull\u7684\u60c5\u51b5\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528<code>@JSONField<\/code>\u8fdb\u884c\u89e3\u91ca\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.appblog.cn\/2018\/02\/03\/FastJson\u53caGson\u5b9a\u4e49\u5b57\u6bb5\u522b\u540d\/\" title=\"FastJson\u53caGson\u5b9a\u4e49\u5b57\u6bb5\u522b\u540d\">FastJson\u53caGson\u5b9a\u4e49\u5b57\u6bb5\u522b\u540d<\/a><\/p>\n<p><!-- more --><\/p>\n<p><code>@JSONField<\/code>\u7684\u4f5c\u7528\u5bf9\u8c61\uff1a<\/p>\n<ol>\n<li>Field<\/li>\n<li>Setter \u548c Getter\u65b9\u6cd5<\/li>\n<\/ol>\n<blockquote>\n<p>\u6ce8\uff1aFastJson\u64cd\u4f5c\u65f6\uff0c\u662f\u6839\u636eGetter\u548cSetter\u7684\u65b9\u6cd5\u8fdb\u884c\u7684\uff0c\u5e76\u4e0d\u662f\u4f9d\u636eField\u8fdb\u884c<\/p>\n<\/blockquote>\n<p>Address.java<\/p>\n<pre><code class=\"language-java\">public class Address {\n    @JSONField(name = &quot;address_id&quot;)\n    private long id;\n    @JSONField(name = &quot;user_id&quot;)\n    private long userId;\n    private String name;\n    private String mobile;\n    private Region country;\n    private Region state;\n    private Region city;\n    private Region district;\n    @JSONField(name = &quot;address_detail&quot;)\n    private String addressDetail;\n    @JSONField(name = &quot;post_code&quot;)\n    private String postcode;\n    @JSONField(serialize = false)\n    private boolean isDefault;\n\n    public Address() {\n    }\n\n    public long getId() {\n        return id;\n    }\n\n    public void setId(long id) {\n        this.id = id;\n    }\n\n    public long getUserId() {\n        return userId;\n    }\n\n    public void setUserId(long userId) {\n        this.userId = userId;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public String getMobile() {\n        return mobile;\n    }\n\n    public void setMobile(String mobile) {\n        this.mobile = mobile;\n    }\n\n    @JSONField(serialize = false)\n    public Region getCountryRegion() {\n        return country;\n    }\n\n    @JSONField(name = &quot;country&quot;)\n    public String getCountry() {\n        return country.getName();\n    }\n\n    @JSONField(serialize = false)\n    public void setCountryRegion(Region country) {\n        this.country = country;\n    }\n\n    @JSONField(name = &quot;country&quot;)\n    public void setCountry(String country) {\n        Region region = new Region();\n        region.setName(country);\n        this.country = region;\n    }\n\n    @JSONField(serialize = false)\n    public Region getStateRegion() {\n        return state;\n    }\n\n    @JSONField(name = &quot;state&quot;)\n    public String getState() {\n        return state.getName();\n    }\n\n    @JSONField(serialize = false)\n    public void setStateRegion(Region state) {\n        this.state = state;\n    }\n\n    @JSONField(name = &quot;state&quot;)\n    public void setState(String state) {\n        Region region = new Region();\n        region.setName(state);\n        this.state = region;\n    }\n\n    @JSONField(serialize = false)\n    public Region getCityRegion() {\n        return city;\n    }\n\n    @JSONField(name = &quot;city&quot;)\n    public String getCity() {\n        return city.getName();\n    }\n\n    @JSONField(serialize = false)\n    public void setCityRegion(Region city) {\n        this.city = city;\n    }\n\n    @JSONField(name = &quot;city&quot;)\n    public void setCity(String city) {\n        Region region = new Region();\n        region.setName(city);\n        this.city = region;\n    }\n\n    @JSONField(serialize = false)\n    public Region getDistrictRegion() {\n        return district;\n    }\n\n    @JSONField(name = &quot;district&quot;)\n    public String getDistrict() {\n        return district.getName();\n    }\n\n    @JSONField(serialize = false)\n    public void setDistrictRegion(Region district) {\n        this.district = district;\n    }\n\n    @JSONField(name = &quot;district&quot;)\n    public void setDistrict(String district) {\n        Region region = new Region();\n        region.setName(district);\n        this.district = region;\n    }\n\n    public String getAddressDetail() {\n        return addressDetail;\n    }\n\n    public void setAddressDetail(String addressDetail) {\n        this.addressDetail = addressDetail;\n    }\n\n    public String getPostcode() {\n        return postcode;\n    }\n\n    public void setPostcode(String postcode) {\n        this.postcode = postcode;\n    }\n\n    public boolean isDefault() {\n        return isDefault;\n    }\n\n    public void setDefault(boolean aDefault) {\n        isDefault = aDefault;\n    }\n}<\/code><\/pre>\n<p>Region.java<\/p>\n<pre><code class=\"language-java\">public class Region {\n    private int id;\n    private String code;\n    private String name;\n    private int parentId;\n\n    public Region() {\n\n    }\n\n    public int getId() {\n        return id;\n    }\n\n    public void setId(int id) {\n        this.id = id;\n    }\n\n    public String getCode() {\n        return code;\n    }\n\n    public void setCode(String code) {\n        this.code = code;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public int getParentId() {\n        return parentId;\n    }\n\n    public void setParentId(int parentId) {\n        this.parentId = parentId;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8eJson\u4e2d\u7684key\u4e0ebean\u4e2d\u7684\u5c5e\u6027\u4e0d\u80fd\u5339\u914d\uff0c\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u90e8\u5206\u5c5e\u6027\u4e3anull\u7684\u60c5\u51b5\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528@JS [&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-694","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\/694","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=694"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}