Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从JSON对象中删除特定属性_C#_Json_C# 4.0_Json.net - Fatal编程技术网

C# 从JSON对象中删除特定属性

C# 从JSON对象中删除特定属性,c#,json,c#-4.0,json.net,C#,Json,C# 4.0,Json.net,我有一个JSON: { "scbs_currentstatus": "", "scbs_primaryissue": "", "_umb_id": "Test", "_umb_creator": "Admin", "_umb_createdate": "0001-01-01 00:00:00", "_umb_updatedate": "0001-01-01 00:00:00", "_umb_doctype": "Te

我有一个JSON:

{
    "scbs_currentstatus": "",
      "scbs_primaryissue": "",
      "_umb_id": "Test",
      "_umb_creator": "Admin",
      "_umb_createdate": "0001-01-01 00:00:00",
      "_umb_updatedate": "0001-01-01 00:00:00",
      "_umb_doctype": "Test",
      "_umb_login": "Test",
      "_umb_email": "Test",
      "_umb_password": {
        "newPassword": "Test",
        "oldPassword": null,
        "reset": null,
        "answer": null
      },
      "_umb_membergroup": {
        " User": false,
        "Line User": true,
        "Callback User": false,
        "Su User": false,
        },
      "umbracoMemberComments": "Test",
      "umbracoMemberFailedPasswordAttempts": ""

    }
我正在尝试删除所有以“”umb\uu“开头的属性。这在json.net中可能吗

输出将如下所示:

{
        "scbs_currentstatus": "",
          "scbs_primaryissue": "",
           "umbracoMemberComments": "Test",
          "umbracoMemberFailedPasswordAttempts": ""
}
使用删除,我可以做到这一点,但不是所有的时间

   result.Property("_umb_id").Remove();

有什么建议吗?

您可以先解析字符串:

var temp =  JArray.Parse(json);
temp.Descendants()
    .OfType<JProperty>()
    .Where(attr => attr.Name.StartsWith("_umb_"))
    .ToList() // you should call ToList because you're about to changing the result, which is not possible if it is IEnumerable
    .ForEach(attr => attr.Remove()); // removing unwanted attributes
json = temp.ToString(); // backing result to json
更新#2

您可以在
where
子句中指定更多条件:

.Where(attr => attr.Name.StartsWith("_umb_") && some_other_condition)


或者你需要的任何东西。

我刚刚结束了对JObject的反序列化,并递归地循环通过它来删除不需要的字段。这是为感兴趣的人准备的函数

private void removeFields(JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return;

List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
    JProperty p = el as JProperty;
    string propertyName = p.hasOwnProperty(key);

    if (p != null && fields.Contains(p.propertyName) && p.propertyName.substring(0,4) == "_umb" )
    {
        removeList.Add(el);
    }
    removeFields(el, fields);
}

foreach (JToken el in removeList)
{
    el.Remove();
}
}
private void removeFields(JToken令牌,字符串[]字段)
{
JContainer container=作为JContainer的令牌;
if(container==null)返回;
List removeList=新列表();
foreach(容器中的JToken el.Children())
{
JProperty p=el作为JProperty;
字符串propertyName=p.hasOwnProperty(键);
if(p!=null&&fields.Contains(p.propertyName)&&p.propertyName.substring(0,4)=“\u umb”)
{
删除列表。添加(el);
}
移除字段(el,字段);
}
foreach(JToken el in removeList)
{
el.移除();
}
}

@javed。我们可以在其中指定多个条件吗?假设属性中的字段不包含“umbracoMember”。
.Where(attr => attr.Name.StartsWith("_umb_") || some_other_condition)
private void removeFields(JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return;

List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
    JProperty p = el as JProperty;
    string propertyName = p.hasOwnProperty(key);

    if (p != null && fields.Contains(p.propertyName) && p.propertyName.substring(0,4) == "_umb" )
    {
        removeList.Add(el);
    }
    removeFields(el, fields);
}

foreach (JToken el in removeList)
{
    el.Remove();
}
}