{"id":435,"date":"2023-12-01T14:42:44","date_gmt":"2023-12-01T06:42:44","guid":{"rendered":"https:\/\/www.tianjm.cn\/?p=435"},"modified":"2023-12-01T14:42:44","modified_gmt":"2023-12-01T06:42:44","slug":"jsonignorejsonconverter%e4%b8%8d%e7%94%9f%e6%95%88%e5%8e%9f%e5%9b%a0","status":"publish","type":"post","link":"https:\/\/www.tianjm.cn\/?p=435","title":{"rendered":"JsonIgnore,JsonConverter\u4e0d\u751f\u6548\u539f\u56e0"},"content":{"rendered":"\n<p class=\"has-light-green-cyan-to-vivid-green-cyan-gradient-background has-background has-medium-font-size\">\u89e3\u51b3\u529e\u6cd5\uff1a\u5f15\u7528 using Newtonsoft.Json<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5728\u8c03\u67e5ajax \u524d\u53f0\u548c\u540e\u53f0\u4ea4\u4e92\u5176\u4e2did\u503c\u4e0d\u4e00\u6837\u7684\u60c5\u51b5\uff0c<\/p>\n\n\n\n<p>\u540e\u53f0\u7ed9\u51fa\u7684\u503c\u662f 16508640061130069 \u524d\u53f0\u63a5\u6536\u5230\u7684\u503c\u662f16508640061130068\uff0c<\/p>\n\n\n\n<p>\u539f\u6765\u662f\u540e\u53f0\u7684id\u7c7b\u578b\u662flong\uff0c\u524d\u53f0js\u6536\u5230\u540e\uff0c\u4f1a\u6709\u7cbe\u5ea6\u4e0a\u7684\u635f\u5931\u5bfc\u81f4\uff0c\u600e\u4e48\u89e3\u51b3\u5462\uff1f\u9700\u8981\u5b9a\u4e49StringJsonConverter\u8f6c\u6362\u7c7b\uff0c\u5c06long\u8f6c\u6362\u6210string\u4e22\u7ed9\u524d\u53f0\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u7cbe\u5ea6\u4e0a\u7684\u4e22\u5931\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public class StringJsonConverter : JsonConverter\r\n    {\r\n        public StringJsonConverter() { }\r\n\r\n        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)\r\n        {\r\n            return reader.Value.ParseToLong();\r\n        }\r\n\r\n        public override bool CanConvert(Type objectType)\r\n        {\r\n            return true;\r\n        }\r\n\r\n        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)\r\n        {\r\n            if (value == null)\r\n            {\r\n                writer.WriteNull();\r\n                return;\r\n            }\r\n            string sValue = value.ToString();\r\n            writer.WriteValue(sValue);\r\n        }\r\n    }<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u5728\u8f6c\u6362\u7684\u5b57\u6bb5\u4e0a\u52a0\u4e0a\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &#91;JsonConverter(typeof(StringJsonConverter))]\r\n        public long? Id { get; set; }<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5728\u524d\u53f0json\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u5c31\u80fd\u5c06id\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8f6c\u6362\u6210string\u7c7b\u578b\u53d1\u9001\u4e86<\/p>\n\n\n\n<p>\u4f46\u662f\u901a\u8fc7\u8fd9\u6837\u8bbe\u7f6e\u540e\uff0c\u5e76\u6ca1\u6709\u751f\u6548\uff0cJsonIgnore\u4e5f\u6ca1\u6709\u751f\u6548\uff0c\u539f\u56e0\u662f\u5f15\u7528\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u9700\u8981\u5f15\u7528 <strong>using Newtonsoft.Json;<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-very-light-gray-to-cyan-bluish-gray-gradient-background has-background\">\u603b\u7ed3\uff1a\u6b64\u95ee\u9898\u80cc\u666f\uff1a\u5728\u505atreetable\u7ed1\u5b9a\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u6570\u636e\u5e76\u6ca1\u6709\u6309\u7167\u5c42\u7ea7\u5173\u7cfb\u7ed1\u5b9a\uff0c\u5728\u5206\u6790js\u540e\uff0c\u53d1\u73b0\u6570\u636e\u6ca1\u6709\u627e\u5230\u6b63\u786e\u7684\u4e0a\u7ea7\uff0c\u8c03\u67e5\u53d1\u73b0\u662f\u540e\u53f0\u7ed9\u7684id\u6570\u636e\u4e0e\u524d\u53f0\u6536\u5230\u7684id\u6570\u636e\u5e76\u4e0d\u4e00\u6837\uff0c\u524d\u53f0\u7684\u6570\u636e\u6709\u7cbe\u5ea6\u4e0a\u7684\u635f\u5931\uff0c\u5bfc\u81f4\u4e0a\u4e0b\u7ea7\u5339\u914d\u5931\u8d25\uff0c\u6309\u7167\u7f51\u4e0a\u7684\u505a\u6cd5\uff0c\u5c06id\u7c7b\u578b\u4ecelong\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u540e\u7ed9\u5230\u524d\u53f0\u53ef\u89e3\u51b3\u95ee\u9898\uff0c\u4e8e\u662f\u53c8\u78b0\u5230\u4e86\u7b2c\u4e8c\u4e2a\u95ee\u9898\uff0c\u5728\u5b9a\u4e49\u548c\u4f7f\u7528 StringJsonConverter \u5c5e\u6027\u540e\uff0c\u5e76\u672a\u751f\u6548\uff0c\u68c0\u67e5\u539f\u56e0\u662f\u5f15\u7528\u7684\u547d\u540d\u7a7a\u95f4\u5bfc\u81f4\uff0c\u6362\u6210\u5236\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u540e\u95ee\u9898\u89e3\u51b3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u5f15\u7528 using Newtonsoft.Json \u5728\u8c03\u67e5a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/posts\/435"}],"collection":[{"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":2,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":437,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions\/437"}],"wp:attachment":[{"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tianjm.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}