直接上解决方法的代码

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

    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; }
}

希望能帮到您解决问题。

.net C# System.Text.Json 如何将 string类型的“true”转换为布尔值 解决方案的更多相关文章

  1. 【译】System.Text.Json 的下一步是什么

    .NET 5.0 最近发布了,并带来了许多新特性和性能改进.System.Text.Json 也不例外.我们改进了性能和可靠性,并使熟悉 Newtonsoft.Json 的人更容易采用它.在这篇文章中 ...

  2. 从 Newtonsoft.Json 迁移到 System.Text.Json

    一.写在前面 System.Text.Json 是 .NET Core 3 及以上版本内置的 Json 序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了 ...

  3. [译]试用新的System.Text.Json API

    译注 可能有的小伙伴已经知道了,在.NET Core 3.0中微软加入了对JSON的内置支持. 一直以来.NET开发者们已经习惯使用Json.NET这个强大的库来处理JSON. 那么.NET为什么要增 ...

  4. 使用System.Text.Json处理Json文档以及部分坑

    System.Text.Json处理Json文档需要用到JsonDocument,JsonElement,JsonProperty. JsonDocument就是一个表示Json文档的东西,JsonE ...

  5. 在.Net Core 3.0中尝试新的System.Text.Json API

    .NET Core 3.0提供了一个名为System.Text.Json的全新命名空间,它支持reader/writer,文档对象模型(DOM)和序列化程序.在此博客文章中,我将介绍它如何工作以及如何 ...

  6. .netcore3.0 System.Text.Json 日期格式化

    .netcore3.0 的json格式化不再默认使用Newtonsoft.Json,而是使用自带的System.Text.Json来处理. 理由是System.Text.Json 依赖更少,效率更高. ...

  7. In .net 4.8,calculate the time cost of serialization in BinaryFormatter,NewtonSoft.json,and System.Text.Json.JsonSerializer.Serialize

    using ConsoleApp390.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; us ...

  8. C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize

    In .net core 3.0 using System;using System.Collections.Generic;using System.Collections;using System ...

  9. .net core中关于System.Text.Json的使用

    在.Net Framework的时候序列化经常使用Newtonsoft.Json插件来使用,而在.Net Core中自带了System.Text.Json,号称性能更好,今天抽空就来捣鼓一下. 使用起 ...

  10. .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法

    行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少 ...

随机推荐

  1. 基于tapd的git commit规范

    现状 开发团队中,总是有人提交代码时的commit内容乱写一通,或者不明确不完整.当回溯代码的时候,很难通过commit内容定位历史记录,只能一条一条查看,找不到就要去问历史参与开发的其他同事,沟通成 ...

  2. 动态规划(六)——树形dp

    树形dp,又称树状dp,即在树上进行的dp,在设计动态规划算法时,一般就以节点从深到浅(子树从小到大)的顺序作为dp的"阶段",dp的状态表示中,第一维通常是节点编号(代表以该节点 ...

  3. 基于 Java 编程生成二维码图片

    0x01 准备 (1)软件版本 IntelliJ IDEA 2023.1.3 JDK 18 Tomcat 10.1.11 Maven 3.8.6 (2)技术栈 servlet zxing 谷歌项目 生 ...

  4. CentOS下修改 MySQL 的密码

    做服务器运维,修改 MySQL 的密码是经常的需要,定期修改 MySQL 密码是网站安全的一个保证.这里记录一下修改 MySQL 密码的一些命令,方便以后查看. 修改root密码 CentOS 下 M ...

  5. 抓包整理————tcpdump过滤器[七]

    前言 简单介绍一下tcpdump 正文 这里可以tcpdump -D 可以列出各个网卡的信息: 默认抓取eth0,也就是第一个: 还有下面的选项: -D 举例所有的网卡设备 -i 选择网卡设备 -c ...

  6. 获取电脑真实的IP地址,忽略虚拟机等IP地址的干扰

    /** * @author yins * @date 2018年8月12日下午9:53:58 */ import java.net.Inet4Address; import java.net.Inet ...

  7. Web前端 -- ES6

    ES标准中不包含 DOM 和 BOM的定义,只涵盖基本数据类型.关键字.语句.运算符.内建对象.内建函数等通用语法. 本部分只学习前端开发中ES6的最少必要知识,方便后面项目开发中对代码的理解. 1. ...

  8. BURP保存多个监听器配置

    "感谢您阅读本篇博客!如果您觉得本文对您有所帮助或启发,请不吝点赞和分享给更多的朋友.您的支持是我持续创作的动力,也欢迎留言交流,让我们一起探讨技术,共同成长!谢谢!" 前言 在进 ...

  9. 力扣28(java)-实现 strStr()(简单)

    题目: 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始).如果不存 ...

  10. [FAQ] Laravel 验证未通过 Route [login] not defined 处理

    一种方式是在路由中定义一个name为 login 的请求. Route::get('xxx', [XxxController::class, 'x'])->name('login'); 第二种方 ...