【问题标题】:How to remove a key value pair within a nested json structure in C#如何在 C# 中删除嵌套 json 结构中的键值对
【发布时间】:2020-10-20 12:31:43
【问题描述】:

我有以下 json

{       
 "audit_date": "2020-05-13T11:27:10.3187798Z",
 "client_uuid": "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e",
 "audit_entry": {
    "where_uri": "test.com/dataservice/apps/171f0841-825b-4964-8f8c-0869650f14a6",
    "why_uri": "test.com/dataservice/reference/reasons_for_change/61acc173-7168-4ae5-9f04- afa228941f8b",
    "who_uri": "test.com/securityservice/users/4977dae1-a307-425f-980c-53413fef1b0f",
    "when_audited": "2018-11-13T20:20:39+00:00",
    "what_uri": "test.com/dataservice/study_subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
    "what_changed": [
        {
            "attribute_name": "birth_year",
            "attribute_value": "1969",
        "attribute_change": "1970"
        },
        {
            "attribute_name": "subject_reference",
            "attribute_value": "TEST-WOO3444",
            "attribute_change": null
        }
      ]
     }
    }

我想删除第二个attribute_change键值对如下

{       
"audit_date": "2020-05-13T11:27:10.3187798Z",
"client_uuid": "2fd77dd8-ed76-4bba-b0e1-5cda454c8d6e",
"audit_entry": {
    "where_uri": "test.com/dataservice/apps/171f0841-825b-4964-8f8c-0869650f14a6",
    "why_uri": "test.com/dataservice/reference/reasons_for_change/61acc173-7168-4ae5-9f04- afa228941f8b",
    "who_uri": "test.com/securityservice/users/4977dae1-a307-425f-980c-53413fef1b0f",
    "when_audited": "2018-11-13T20:20:39+00:00",
    "what_uri": "test.com/dataservice/study_subjects/1bc67a71-8549-4ab8-9dd9-e44238198860",
    "what_changed": [
        {
            "attribute_name": "birth_year",
            "attribute_value": "1969",
        "attribute_change": "1970"
        },
        {
            "attribute_name": "subject_reference",
            "attribute_value": "TEST-WOO3444",
            
        }
      ]
     }
    }

我试过下面的代码

        JObject jObject = JObject.Parse(jsonText);
        JObject jObj = (JObject)jObject.SelectToken("audit_entry");

        //remove second attribute changed token
        jObj.Property("what_changed")("attribute_change")[1].Remove();

        string json = jObj.ToString(Formatting.None);

我知道 jObj.Property 的语法是错误的,但经过几个小时的谷歌搜索后,我找不到答案。 非常感谢任何帮助。

【问题讨论】:

标签: c# json json.net


【解决方案1】:

由于what_changed 是一个数组,您应该强制转换为JArray 以便能够访问其元素并删除attribute_change 令牌

var jObject = JObject.Parse(jsonText);

if (jObject["audit_entry"]?["what_changed"] is JArray array)
    if (array[1] is JObject attribute)
        attribute.Remove("attribute_change");

Console.WriteLine(jObject);

【讨论】:

  • 你是个天才!完美运行。即使经过数小时的尝试,我也永远不会得到它。非常感谢我的朋友。
  • @RShome 欢迎您,如果有帮助,您可以投票或批准答案
  • 绝对!非常感谢您为我节省了数小时的痛苦..
【解决方案2】:

搜索开始属性名称及其结尾。然后将之前/之后的json字符串连接成一个新字符串。也许是一种 hack,但它会让你继续编码,直到你找到更优雅的解决方案。

【讨论】:

    【解决方案3】:

    您可以通过调用具有相应属性名称的Remove 方法从JObject 中删除一个属性。例如:

    JObject jObject = JObject.Parse(jsonText);
    
    // find required object, there are other options
    // for example (JObject)jObject["audit_entry"]["what_changed"][1]
    var nested = (JObject)jObject.SelectToken("$.audit_entry.what_changed[1]"); 
    
    //remove attribute changed token
    nested.Remove("attribute_change");
    string json = jObject.ToString(Formatting.None); // attribute_change is removed from jObject
    

    如果您想删除所有具有此类名称和 null 值的属性,您可以执行以下操作:

            var toRemove = jObject.Descendants()
                .OfType<JProperty>()
                .Where(prop => prop.Name == "attribute_change" && prop.Value.Type == JTokenType.Null)
                .ToList();
            foreach (var prop in toRemove)
            {
                prop.Remove();
            }
    

    【讨论】:

    • @RShome 很乐意提供帮助!
    猜你喜欢
    • 2019-08-13
    • 1970-01-01
    • 2016-08-19
    • 2019-10-17
    • 1970-01-01
    • 2021-12-25
    • 2017-10-02
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多