【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}");
}
相关推荐
头发够用的程序员18 分钟前
C++和Python面试经典算法汇总(一)
开发语言·c++·python·算法·容器·面试
夜猫逐梦30 分钟前
【逆向经验】一篇文章讲透为什么CE搜不到Python游戏的内存值
开发语言·python·游戏
超梦dasgg1 小时前
智慧充电系统设备管理服务对外接口实现方案
java·spring·微服务
SilentSamsara1 小时前
闭包的本质:Python 如何捕获自由变量
开发语言·python·青少年编程·pycharm
十五年专注C++开发1 小时前
浅谈LLVM
开发语言·c++·qt·clang·llvm
xiaoye37081 小时前
Spring 事务传播机制 + 隔离级别
java·后端·spring
白夜11172 小时前
C++(标签派发 Tag Dispatching)
开发语言·c++·笔记·算法
Arya_aa2 小时前
数据字典模块–JSR303参数校验
java
CSCN新手听安2 小时前
【Qt】Qt窗口(六)QMessageBox消息对话框的使用
开发语言·c++·qt
明月(Alioo)2 小时前
给 AI Agent 装上“大脑“:Java语言中Code Interpreter 的设计与实现
java·ai·agent