是否可以从JObject变量中删除字符串值的JSON节点?

编程入门 行业动态 更新时间:2024-10-09 17:29:44
本文介绍了是否可以从JObject变量中删除字符串值的JSON节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用.NET 4.7,MVC5,C#和JSON.NET

I am using .NET 4.7, MVC5, C# and JSON.NET

我有一个名为"json"的JObject变量

I have a JObject variable called "json"

JObject jsonPerson = JObject.Parse(strPersonsDetails);

它包含名称相同但结构不同的节点:

It contains nodes with same name, but different structure:

name : "Joe Bloggs"

name : { firstname : "Joe", lastname : "Bloggs" }

我想删除任何字符串类型的节点,即:

I would like to remove any node that are of type string ie :

name : "Joe Bloggs"

离开:

name : { firstname : "Joe", lastname : "Bloggs" }

名称"类型的

是否可以从jsonPerson中删除所有具有文本值的属性,而不是具有"name"类型的复杂类型的其他版本?

Is there any way to remove all properties called "name" which have a text value as opposed to the other version that has a complex type of type "name" from jsonPerson?

更完整的JSON:

{ "items": [ { "id" : 1, "name" : "Joe Bloggs" }, { "id" : 2, "name" : { "FirstName" : "Joe", "LastName" : "Bloggs" } } ] }

推荐答案

您可以结合使用 JSONPath 查询以选择JSON层次结构中所有相关的"name"节点的值,然后过滤那些 JToken.Type 是 JTokenType.String

You can combine a JSONPath query to select the values of all relevant "name" nodes in your JSON hierarchy, then filter those for which JToken.Type is JTokenType.String. Then you can remove them from their parent JObject by removing the containing JProperty.

以下代码可以完成这项工作:

The following code does the job:

// Select the values of all relevant "name" nodes using a JSONPath query, // goessner/articles/JsonPath/ // Then select only those whose which are of type string. var query = jsonPerson .SelectTokens("..name") .Where(t => t.Type == JTokenType.String); query.ToList().ForEach(t => t.RemoveFromLowestPossibleParent());

使用扩展方法:

public static partial class JsonExtensions { public static JToken RemoveFromLowestPossibleParent(this JToken node) { if (node == null) return null; // If the parent is a JProperty, remove that instead of the token itself. var contained = node.Parent is JProperty ? node.Parent : node; contained.Remove(); // Also detach the node from its immediate containing property -- Remove() does not do this even though it seems like it should if (contained is JProperty) ((JProperty)node.Parent).Value = null; return node; } }

演示小提琴此处.

注意:

  • 我使用JSONPath递归下降操作符..在JSON层次结构中的任何位置选择了名为name的 all 属性的值.假设您的JSON具有相当固定的架构,则可能需要简化以选择name节点在层次结构中的预期位置:

  • I used the JSONPath recursive descent operator .. to select the values of all properties named name anywhere in the JSON hierarchy. Assuming your JSON has a fairly fixed schema you might want to simplify that to select the name nodes at their expected location in the hierarchy:

var query = jsonPerson .SelectTokens("items[*].name") .Where(t => t.Type == JTokenType.String);

其中*是通配符运算符,用于选择所有数组条目.演示小提琴#2 此处.

Where * is the wildcard operator selecting all array entries. Demo fiddle #2 here.

SelectTokens 返回所选属性值.为了从层次结构中删除这些值,必须删除包含的JProperty.扩展方法JsonExtensions.RemoveFromLowestPossibleParent()可以做到这一点.

SelectTokens returns the selected property values. In order to remove those values from the hierarchy it is necessary to remove the containing JProperty. The extension method JsonExtensions.RemoveFromLowestPossibleParent() does this.

更多推荐

是否可以从JObject变量中删除字符串值的JSON节点?

本文发布于:2023-11-28 20:29:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   变量   字符串值   JObject   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!