C# JSON.NET如何删除节点
我有一个json,如下所示:C# JSON.NET如何删除节点,c#,json,json.net,jsonpath,C#,Json,Json.net,Jsonpath,我有一个json,如下所示: { "d": { "results": [ { "__metadata": { }, "prop1": "value1", "prop2": "value2", "__some": "value" }, { "__metadata": { }, "prop3": "value1",
{
"d": {
"results": [
{
"__metadata": {
},
"prop1": "value1",
"prop2": "value2",
"__some": "value"
},
{
"__metadata": {
},
"prop3": "value1",
"prop4": "value2",
"__some": "value"
},
]
}
}
我只是想把这个JSON转换成一个不同的JSON。我想从JSON中去掉“\u元数据”和“\u一些”节点。我使用的是JSON.NET。我将创建一个只包含所需信息的新数据结构,并从第一个数据结构复制数据。通常这是最简单的方法。这只是一个想法。我刚刚结束了对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;
if (p != null && fields.Contains(p.Name))
{
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;
if(p!=null&&fields.Contains(p.Name))
{
删除列表。添加(el);
}
移除字段(el,字段);
}
foreach(JToken el in removeList)
{
el.移除();
}
}
根据@[Mohamed Nuur]的答案,我将其改为一种扩展方法,我认为这种方法效果更好:
public static JToken RemoveFields(this JToken token, string[] fields)
{
JContainer container = token as JContainer;
if (container == null) return token;
List<JToken> removeList = new List<JToken>();
foreach (JToken el in container.Children())
{
JProperty p = el as JProperty;
if (p != null && fields.Contains(p.Name))
{
removeList.Add(el);
}
el.RemoveFields(fields);
}
foreach (JToken el in removeList)
{
el.Remove();
}
return token;
}
如果您的JArray带有JTokens,而不是JObjects,则此答案适用: 以下是一个例子:
string json = "[null, null, \"x\", null, null, null, 0,[],[[\"x\"], null,[0],[\"x\"]]]";
JArray array = JArray.Parse(json);
// Keep first 3 elements, remove the rest
int max = array.Count;
for (int i = 0; i < max - 3; i++)
{
JToken elem = array[3];
array.Remove(elem);
}
json = array.ToString(Newtonsoft.Json.Formatting.None);
Console.WriteLine(json);
string json=“[null,null,\“x\”,null,null,null,0,[],[[\“x\”],null,[0],\“x\”]]”;
JArray数组=JArray.Parse(json);
//保留前3个元素,删除其余元素
int max=array.Count;
对于(int i=0;i
我没有使用C#的经验,但我假设您可以简单地解析JSON,从生成的数据结构中删除它们的键,然后将其重新转换为JSON。我可以这样做,但我正在寻找JSON.NET的方法。但我不知道数据将是什么。我想编写一个通用函数,它将接受一个JSON字符串和一个要排除的属性列表(可能是xpath类型?),并返回一个新的JSON字符串,而不包含排除的属性。谢谢!我需要这个。现在,我可以在JArray的Children集合上执行Foreach,以删除所有我想要的属性。这确实帮助了我——我想用敏感字段替换JSON数据的长度。我能够获得并设置p.Value。伟大的解决方案,经过测试和使用
string json = "[null, null, \"x\", null, null, null, 0,[],[[\"x\"], null,[0],[\"x\"]]]";
JArray array = JArray.Parse(json);
// Keep first 3 elements, remove the rest
int max = array.Count;
for (int i = 0; i < max - 3; i++)
{
JToken elem = array[3];
array.Remove(elem);
}
json = array.ToString(Newtonsoft.Json.Formatting.None);
Console.WriteLine(json);