【JToken】JToken == null 判断无效的问题

复制代码
  if (innerNode == null) 
            {
                continue; 
            }
  Debug.Log($"toNode type: {node["toNode"]?.GetType()}");

发现这个JToken 无法正确的判断 是否为 null,再排除逻辑问题后,我基本能确定的是 这个对象 不返回的不是真正的C# NULL

输出类型后是 Newtonsoft.Json.Linq.JValue

可知 最终 JValue 的 null 不能等于C# 的null

所以

JValue 的特殊性:

当 JSON 中显式定义 "toNode": null 时,Newtonsoft.Json 会将其解析为 JValue 类型(而非真正的 null)。

此时 node"toNode" 是一个 JValue 对象,其 Value 属性为 null,但对象本身非 null。

因此 if (innerNode == null) 会返回 false(因为 JValue 对象存在),导致 continue 未触发。

复制代码
JToken innerNode = next ? node["toNode"] : node;
if (innerNode == null || innerNode.Type == JTokenType.Null) 
{
    continue; // 正确处理显式 JSON null 值
}
string nodeId = node["id"]?.ToString();
if (nodeId != null)
{
    Debug.Log($"{nodeId},{innerNode},{next}");
}
相关推荐
dadaobusi3 小时前
学习:RV lkvm SBI
java·学习·spring
djarmy5 小时前
MAIN.c(1): warning C318: can’t open file ‘STC8G.H’ 报错 解决 分析
c语言·开发语言·mongodb
HEJOO96 小时前
深入理解 Java volatile 关键字:原理、用法与常见误区
java·开发语言·spring
曹牧6 小时前
Java:no content to map due to end of input
java·开发语言
阿维的博客日记6 小时前
Example 类全场景实战指南
java·mybatis
梦梦代码精6 小时前
《回收租赁系统技术选型避坑指南:业务闭环与二开自由度详解》
开发语言·低代码·docker·开源·代码规范
两点王爷6 小时前
SpringBoot 项目集成 GeoServer 源码,实现地图服务发布
java·spring boot·后端
隔窗听雨眠6 小时前
记一次SQL Server数据库性能分析:从CPU100%到单配置修复的完整诊断
开发语言·数据库·php
David猪大卫6 小时前
【C++修炼】智能指针使用及原理
开发语言·c++·经验分享·笔记·学习·考研·面试
charlie1145141916 小时前
现代Qt开发之图像处理进阶:像素操作与格式转换
开发语言·图像处理·qt