You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中移除JObject的指定属性(如filed1)?

C#中移除JObject指定属性的实现方法

首先,确保你的项目已引用Newtonsoft.Json库(可通过NuGet包管理器安装),JObject是该库的核心类型。

原始JObject实例

{ "filed1": "4", "filed2": "name", "filed3": null, "filed4": "2021-09-20T00:00:00Z", "filed5": null, "filed6": null, "filed7": null }

实现代码

直接调用JObject的Remove方法即可移除指定属性:

using Newtonsoft.Json.Linq;

// 初始化JObject实例(若已有现成实例可跳过此步)
JObject targetObj = JObject.Parse(@"{ ""filed1"": ""4"", ""filed2"": ""name"", ""filed3"": null, ""filed4"": ""2021-09-20T00:00:00Z"", ""filed5"": null, ""filed6"": null, ""filed7"": null }");

// 移除filed1属性
bool isRemoved = targetObj.Remove("filed1");

// 可选:验证移除结果
Console.WriteLine(targetObj.ToString());

补充说明

  • Remove方法返回布尔值:true表示属性存在并成功移除,false表示目标属性不存在。
  • 即使目标属性不存在,调用Remove也不会抛出异常;若需提前判断属性是否存在,可使用ContainsKey方法。

内容的提问来源于stack exchange,提问作者deee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 20:11:09