gpt4 book ai didi

c# - 删除 JArray 的成员

转载 作者:行者123 更新时间:2023-12-02 03:22:46 24 4
gpt4 key购买 nike

我有以下 JSON

[
{
"Code": "Global Payroll",
"Month1": 1,
"Month2": 0,
"Month3": 0,
"Month4": null,
"Month5": null,
"Month6": null,
"Month7": null,
"Month8": null,
"Month9": null,
"Month10": null,
"Month11": null,
"Month12": null,
"YTD": 1,
"PercentOfTotal": "16.67%"
},
{
"Code": "GV Payroll",
"Month1": 0,
"Month2": 0,
"Month3": 3,
"Month4": null,
"Month5": null,
"Month6": null,
"Month7": null,
"Month8": null,
"Month9": null,
"Month10": null,
"Month11": null,
"Month12": null,
"YTD": 3,
"PercentOfTotal": "50.00%"
}
]

我想要做的是以某种方式从 JSON 中删除 Month4、Month5 等,然后将其转换回字符串。

我尝试查看 JArray.remove 方法,但这会从数组中删除项目本身。有人可以告诉我如何从 JArray 中完全删除属性吗?

最佳答案

var array = JArray.Parse(json);

foreach (JObject elem in array)
{
foreach (var elementToRemove in new List<string>() {"Month4", "Month5" })
{
elem.Property(elementToRemove).Remove();
}
}

var resultJson = array.ToString();

关于c# - 删除 JArray 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54298392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com