C# JSON.NET 如何删除节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11676159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.NET how to remove nodes
提问by Mohamed Nuur
I have a json like the following:
我有一个像下面这样的json:
{
"d": {
"results": [
{
"__metadata": {
},
"prop1": "value1",
"prop2": "value2",
"__some": "value"
},
{
"__metadata": {
},
"prop3": "value1",
"prop4": "value2",
"__some": "value"
},
]
}
}
I just want to transform this JSON into a different JSON. I want to strip out the "_metadata" and "_some" nodes from the JSON. I'm using JSON.NET.
我只是想把这个 JSON 转换成不同的 JSON。我想从 JSON 中去除“ _ metadata”和“_some”节点。我正在使用 JSON.NET。
采纳答案by Mohamed Nuur
I just ended up deserializing to JObject and recursively looping through that to remove unwanted fields. Here's the function for those interested.
我刚结束反序列化到 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();
}
}
回答by Anders Lindén
I would create a new data structure with only the required information and copy the data from the first one. Often that is the simpliest approach. Just an idea.
我将创建一个仅包含所需信息的新数据结构,并从第一个数据结构复制数据。通常这是最简单的方法。只是一个想法。
回答by Rafi
Building off of @[Mohamed Nuur]'s answer, I changed it to an extension method which I think works better:
基于@[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;
}
Here is unit test:
这是单元测试:
[TestMethod]
public void can_remove_json_field_removeFields()
{
string original = "{\"d\":{\"results\":[{\"__metadata\":{},\"remove\":\"done\",\"prop1\":\"value1\",\"prop2\":\"value2\",\"__some\":\"value\"},{\"__metadata\":{},\"prop3\":\"value1\",\"prop4\":\"value2\",\"__some\":\"value\"}],\"__metadata\":{\"prop3\":\"value1\",\"prop4\":\"value2\"}}}";
string expected = "{\"d\":{\"results\":[{\"prop1\":\"value1\",\"prop2\":\"value2\",\"__some\":\"value\"},{\"prop3\":\"value1\",\"prop4\":\"value2\",\"__some\":\"value\"}]}}";
string actual = JToken.Parse(original).RemoveFields(new string[]{"__metadata", "remove"}).ToString(Newtonsoft.Json.Formatting.None);
Assert.AreEqual(expected, actual);
}
回答by live-love
This answer applies if you have a JArray with JTokens, not JObjects:
如果您的 JArray 带有 JTokens 而不是 JObjects,则此答案适用:
Here is an example:
下面是一个例子:
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);

