【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}");
}
相关推荐
x***381615 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
故事不长丨15 小时前
C#定时器与延时操作的使用
开发语言·c#·.net·线程·定时器·winform
hefaxiang15 小时前
C语言常见概念(下)
c语言·开发语言
S***848815 小时前
SpringSecurity踢出指定用户
java
p***s9115 小时前
Spring数据库原理 之 DataSource
java·数据库·spring
adobehu15 小时前
麒麟系统安装jdk17
java·jdk
欧阳天风15 小时前
js实现鼠标横向滚动
开发语言·前端·javascript
spencer_tseng15 小时前
java.util.IllegalFormatPrecisionException
java·printf
虹科网络安全15 小时前
艾体宝干货 | Redis Java 开发系列#1 从零开始的环境搭建与实践指南
java·数据库·redis
铅笔侠_小龙虾16 小时前
Arthas 命令
java·jvm