gpt4 book ai didi

c# - 从大型 JSON 中删除属性,c#

转载 作者:行者123 更新时间:2023-12-04 09:01:48 25 4
gpt4 key购买 nike

我收到 JSON,我需要从所有响应中删除一个属性“original_0”。我正在研究 C#。我不确定如何删除该属性。
下面是 JSON

{
"entity": [{
"JobId": 725454,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T18:30:19.000",
"End Time": "2020-08-21T18:32:14.000",
"Duration in Mins": 2,
"Size of Application": 56623104,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "2",
"serverName": "sgkcovldidbdev01"
}
},
{
"JobId": 724699,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T12:30:09.000",
"End Time": "2020-08-21T12:31:09.000",
"Duration in Mins": 1,
"Size of Application": 95420416,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "2",
"serverName": "sgkcovldidbdev01"
}
},
{
"JobId": 724056,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T06:30:11.000",
"End Time": "2020-08-21T06:31:10.000",
"Duration in Mins": 1,
"Size of Application": 333447168,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "2",
"serverName": "sgkcovldidbdev01"
}
},
{
"JobId": 723035,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T00:30:10.000",
"End Time": "2020-08-21T00:31:11.000",
"Duration in Mins": 1,
"Size of Application": 115343360,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0,
"original_0": {
"numberOfDays": "2",
"serverName": "sgkcovldidbdev01"
}
}
]}
删除“original_0”后,JSON 应如下所示。
{
"entity": [{
"JobId": 725454,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T18:30:19.000",
"End Time": "2020-08-21T18:32:14.000",
"Duration in Mins": 2,
"Size of Application": 56623104,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0

},
{
"JobId": 724699,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T12:30:09.000",
"End Time": "2020-08-21T12:31:09.000",
"Duration in Mins": 1,
"Size of Application": 95420416,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0
},
{
"JobId": 724056,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T06:30:11.000",
"End Time": "2020-08-21T06:31:10.000",
"Duration in Mins": 1,
"Size of Application": 333447168,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0
},
{
"JobId": 723035,
"Operation Type": "Backup",
"Client": "sgkcovldidbdev01",
"Agent": "Oracle Database",
"Instance": "INFSGDEV",
"BackupSet": "default",
"Subclient": "ArchiveLog",
" Backup Type": "Full",
"Start Time": "2020-08-21T00:30:10.000",
"End Time": "2020-08-21T00:31:11.000",
"Duration in Mins": 1,
"Size of Application": 115343360,
"Job Status": "Completed",
"dt": "2020-08-21T00:00:00.000",
"age": 0
}
]}
我正在使用以下代码删除该属性,但它不起作用。
var jsonRes = JsonConvert.DeserializeObject<dynamic>(json);

foreach (var item in jsonRes)
{
item.Property("original_0").Remove();
// Console.WriteLine(item.ToString());
}
谢谢

最佳答案

您可以使用以下命令从 json 文件中删除所有属性:

            var json = File.ReadAllText(@"YOUR_PATH_HERE\json1.json");
var jObj = JObject.Parse(json);

foreach (JObject jObj in jObj["entity"].Children<JObject>())
{
JProperty originalJProperty = jObj.Property("original_0");
originalJProperty?.Remove();
}

关于c# - 从大型 JSON 中删除属性,c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63543773/

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