wpf C#使用newtonsoft删除json子节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28391303/
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
C# remove json child node using newtonsoft
提问by siva prasanna
I am developing an app using c# wpf in .net 3.5. I use newtonsoft library to parse the json string.
我正在 .net 3.5 中使用 c# wpf 开发应用程序。我使用 newtonsoft 库来解析 json 字符串。
I want to know how to remove a child node of json.
我想知道如何删除json的子节点。
For example, My json data =
例如,我的 json 数据 =
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}]}
The function
功能
jobject.Remove("employees");
removes all the nodes sucessfully
成功删除所有节点
I would like to know how to remove the first employee detail alone.
我想知道如何单独删除第一个员工详细信息。
采纳答案by gaiazov
Once you parse your json into a JObject, the employees property will be a JArray. The JArrayclass has methods you're looking for, such as JArray.RemoveAt
将 json 解析为 a 后JObject,员工属性将是 a JArray。该JArray班有你要找的方法,如JArray.RemoveAt
The following code will do what you want
以下代码将执行您想要的操作
string json =
@"{
""employees"":[
{ ""firstName"":""John"", ""lastName"":""Doe""},
{ ""firstName"":""Anna"", ""lastName"":""Smith""},
{ ""firstName"":""Peter"", ""lastName"":""Jones""}
]
}";
dynamic obj = JObject.Parse(json);
(obj.employees as JArray).RemoveAt(0);
// obj now only has "Anna Smith" and "Peter Jones"
dynamicwas introduced in .NET 4.0, so for 3.5 you'd use something like this instead
dynamic是在 .NET 4.0 中引入的,因此对于 3.5,您可以使用类似这样的东西
JObject obj = JObject.Parse(json);
(obj["employees"] as JArray).RemoveAt(0);

