WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

直接上解决方法的代码

先定义一个转换顺,代码如下:

复制代码
    public sealed class AnhBoolConverter : JsonConverter<bool?>
    {
        public override bool? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var val = JsonSerializer.Deserialize<string?>(ref reader, options);
            bool? boolVal = Convert.ToBoolean(val);
            return boolVal;
        }

        public override void Write(Utf8JsonWriter writer, bool? val, JsonSerializerOptions options)
            => // What do I do here? I want to preserve other options such as options.PropertyNamingPolicy, which are lost by the following call
            JsonSerializer.Serialize(writer, val);
    }
复制代码

再到需要反序列化地实体上添加如下属性(黄色高亮区域):

复制代码
    public class ResponseBase
    {
        /// <summary>
        /// 接口请求结果  true or false
        /// 示例值:"result": "true" 或 true
        /// </summary>
        [System.Text.Json.Serialization.JsonConverter(typeof(AnhBoolConverter))]
        public bool? result { set; get; }

        /// <summary>
        /// 系统返回结果描述
        /// 示例值:接口调用成功
        /// </summary>
        public string? msg { set; get; }

        /// <summary>
        /// 错误码
        /// </summary>
        public string? errCode { get; set; }
    }
复制代码

 

希望能帮到您解决问题。

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2022-12-20 .net core 5,6,7【多线程笔记】取消令牌(CancellationToken) CancellationTokenSource
2018-12-20 nginx 将请求全部指向到一个页面
2018-12-20 windows10下面部署nginx(解决文件名中文乱码问题)
2018-12-20 二.Nginx反向代理和静态资源服务配置
2018-12-20 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能
2018-12-20 Nginx的使用(一)代理静态文件
2018-12-20 如何让NGINX显示文件夹目录
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多