c# - 删除 JArray 的成员
问题描述
我有以下 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();
推荐阅读
- reactjs - 如何在没有 GraphQL 的情况下正确使用页面组件
- swift - 触摸单元格时如何调用复选框按钮的操作?
- python-3.x - 获取word docx中的表格数量
- bash - 无法从脚本中获取任何 Docker 入口点而无需连续重启
- karate - 如何使用 karateoptions 将范围报告与空手道框架集成。任何示例实现?
- css - 没有单独背景包装的 CSS 背景动画
- php - 如何使用 PHP 设置 div 的背景?
- python - python - 如何根据python pandas数据框中的value_counts将二进制列(1,0)替换为(0,1)?
- ios - 滑动我的收藏视图时, hidesBarsOnSwipe 方法不起作用
- javascript - .Net Core 2.2 - 活动目录:在 javascript 上获取用户名