如何在C#中清空JObject数组 [英] How to empty a JObject array in C#

查看:789
本文介绍了如何在C#中清空JObject数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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": null
        },
        {
            "attribute_name": "subject_reference",
            "attribute_value": "TEST-WOO3444",
            "attribute_change": null
        }
    ]
   }
}

但是我想清空嵌套数组"what_changed"

But I want to empty the nest array "what_changed"

所以我需要输出

{       
"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": []
  }
}

我写了以下代码

        JObject jObj = JObject.Parse(jsonText);
        jObj["audit_entry"]["what_changed"] = null;
        string json = jObj.ToString(Formatting.None);

但这会使字段为空而不是空数组.

but this makes the field null rather than empty array.

我也尝试过

        JObject jObj = JObject.Parse(jsonText);
        jObj["audit_entry"]["what_changed"] = "";
        string json = jObj.ToString(Formatting.None);

但是那仍然不能给出一个空数组.

but that still doesn't give an empty array.

我也尝试使用Array.Clear()方法,但这是一个JObject数组,而不是普通数组.

I also tried using the Array.Clear() method, but this is a JObject array rather than a normal array.

推荐答案

数组由 JArray 类型,而不是 JObject .将"what_changed"的值强制转换为正确的类型,然后使用所需的方法.例如:

Arrays are represented by the JArray type, not JObject. Cast the value of "what_changed" to the proper type and use the methods you need. Eg:

JObject jObj = JObject.Parse(jsonText);
JArray changed=(JArray)(jObj["audit_entry"]["what_changed"]);
changed.Clear();

使用JSON元素虽然很少见.将JSON字符串反序列化为强类型对象,然后根据需要对其进行修改,然后将其序列化回字符串,通常要容易得多.

Working with JSON elements is rather unusual though. It's typically a lot easier to deserialize JSON strings into strongly typed objects, modify them as needed and then serialize them back to a string.

通过在Edit菜单中选择Paste Special > Paste JSON as Classes,可以轻松地在Visual Studio中生成必要的DTO.

Generating the necessary DTOs can be done easily in Visual Studio by selecting Paste Special > Paste JSON as Classes from the Edit menu

这篇关于如何在C#中清空JObject数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆