简体   繁体   English

从 C# 中的 JSON 响应中删除所有出现的特定键

[英]Remove all occurrences of a particular key from a JSON response in C#

I have a JSON string from which I want to eliminate all the occurrences of a given key.我有一个 JSON 字符串,我想从中消除给定键的所有出现。

JSON I have: JSON 我有:

string requstBody =       
{
              "payLoad": [
                {
                  "BaseVersionId_": 9,
                  "VersionId_": 10,              
                  "AssetCollateralLink": [
                    {
                      "AssetId": 137,
                      "BaseVersionId_": 9,
                      "VersionId_": 10
                    },
                    {
                      "AssetId": 136,
                      "BaseVersionId_": 0,
                      "VersionId_": 1
                    }
                  ],
                  "CollateralProvider": [],
                  "AdvCollateralAllocation": [
                    {
                      "LinkId": 91,
                      "IsDeleted_": false,
                      "BaseVersionId_": 1,
                      "VersionId_": 2
                    }
                  ]
                }
              ]
            }

I want to eliminate keys "BaseVersionID_" and "VersionId_" as follows:我想消除键“BaseVersionID_”和“VersionId_”,如下所示:

  string requstBody =  
{
              "payLoad": [
                {
                  "AssetCollateralLink": [
                    {
                      "AssetId": 137
                    },
                    {
                      "AssetId": 136
                    }
                  ],
                  "CollateralProvider": [],
                  "AdvCollateralAllocation": [
                    {
                      "LinkId": 91,
                      "IsDeleted_": false
                    }
                  ]
                }
              ]
            }

I used JObject.Remove();我使用了 JObject.Remove(); as follows如下

JObject sampleObj1 = new JObject();
sampleObj1 = JsonHelper.JsonParse(requestBody);
sampleObj1.Remove("BaseVersionId_");

but able to remove the keys under payLoad Hierarchy only.但只能删除payLoad Hierarchy下的键。 How do I remove all the occurrences of the Key.如何删除所有出现的密钥。

The required properties can be removed from the Json , simply with Linq:可以从Json删除所需的属性,只需使用 Linq:

var jsonObj = JObject.Parse(requestBody);
jsonObj.SelectToken("payLoad[0]").SelectToken("AdvCollateralAllocation")
                .Select(jt => (JObject)jt)
                .ToList()
                .ForEach(r =>
                    r
                    .Properties()
                    .ToList()
                    .ForEach(e =>
                    {
                        if (e.Name == "BaseVersionId_" || e.Name == "VersionId_")
                            e.Remove();
                    }));

The resultant jsonObj will be without the BaseVersionId_ and VersionId_ names as well as their values.结果jsonObj将没有BaseVersionId_VersionId_名称及其值。

I'd use JsonPath as such:我会像这样使用 JsonPath:

var toRemove =  jsonObject
                .SelectTokens("$.payLoad.[*].AssetCollateralLink.[*]..BaseVersionId_")
                .Concat(stuff.SelectTokens("$.payLoad.[*].AssetCollateralLink.[*]..VersionId_"))
                .Concat(stuff.SelectTokens("$.payLoad.[*].AdvCollateralAllocation.[*]..VersionId_"))
                .Concat(stuff.SelectTokens("$.payLoad.[*].AdvCollateralAllocation.[*]..BaseVersionId_"))
                .ToList();

for (int i = toRemove.Count - 1; i >= 0; i--)
{
    toRemove[i].Parent?.Remove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM