从分层JSON第二部分挑选出简单性质 [英] Picking Out Simple Properties from Hierarchical JSON Part II

查看:104
本文介绍了从分层JSON第二部分挑选出简单性质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个问题可能是病态的,所以我创建了一个妹妹的问题这是远远更多了点,它特定的输出引用。请看到:
查询JSON嵌套数组使用LINQ,JSON.NET,C#
。如果这个问题得到这个回答过,我会尝试使用来自其他有关资料来回答这一个我自己... :)谢谢!

I thought this question might be ill-formed, so I created a "sister" question that's far more to-the-point, which specific output cited. Please see to: Querying JSON Nested Arrays with Linq, JSON.NET, C# If that question gets answered before this one, I'll try to answer this one myself using the information from the other question... :) Thanks!

在先前的问题(挑选从分层JSON 简单的属性),我问怎么捡起层次JSON简单属性。答案有[粘贴于本岗位的末端LINQ查询]为extremently有用的(并且,因为张贴的,我已经看了不少关于LINQ和JSON.NET)。所以,因为我懒我没有使用这个论坛 - 我使用它时,我真的卡住,似乎无法找到的书,我有机会获得答案。

In a previous question (Picking Out Simple Properties from Hierarchical JSON), I asked how to pick up simple properties from hierarchical JSON. The answer there [pasted as a Linq query at the end of this post] is extremently useful (and, since posting that, I've studied quite a bit about Linq and JSON.NET). So I'm not using this forum because I'm lazy--I use it when I'm really stuck and can't seem to find the answers in the books I have access to.

我难倒就如何继续与我刚才的问题提供了范例。这个问题是建立在上一个,所以这里(尽量简洁,我可以表达)是我很想学习如何在一个单一的Linq查询做。

I'm stumped on how to continue with the example provided by my previous question. This question builds on the previous one, so here (as succinctly as I could express) is what I'd love to learn how to do in a single Linq query.

总括来说:

{
    "Branch1": {
        "Prop1A": "1A",
        "Prop1B": "1B",
        "Prop1C": "1C",
        "Branch2": {
            "Prop2A": "2A",
            "Prop2B": "2B",
            "Prop2C": "2C",
            "Branch3": [{
                "Prop3A": "3A",
                "Prop3B": "3B",
                "Prop3C": "3C"
            }, {
                "Prop3D": "3D",
                "Prop3E": "3E",
                "Prop3F": "3F"
            }, {
                "Prop3G": "3G",
                "Prop3H": "3H",
                "Prop3I": "3I"
            }]
        },
        "Branch4": [{
            "Prop4A": "4A",
            "Prop4B": "4B",
            "Prop4C": "4C"
        }, {
            "Prop4E": "4E",
            "Prop4F": "4F",
            "Prop4G": "4G"
        }, {
            "Prop4H": "4H",
            "Prop4I": "4I",
            "Prop4I": "4I"
        }]
    }
}

正如你所看到的,动态的JSON是由分层的对象,这些对象是JSON对象,JSON数组,JSON属性。

As you can see, the dynamic JSON is composed of hierarchical objects, and these objects are JSON objects, JSON arrays, and JSON properties.

最后,我想这个JSON转换为一个List对象,我可以在C#中一起工作。我打算使用List对象有效地处理文档顺序每个JSON分支从顶部下跌。

Ultimately, I want to convert this JSON to a List object I can work with in C#. I plan to use that List object to effectively process each JSON branch in document order from the top, down.

列表集合中的每个项目将是一个JObject;每个对象将有一个合成的父的字符串属性,将指向回支下了JObject出现在原来的JSON(我在下面的例子解释一下我的意思是父)。 [前一个问题正确地想出了这个父的价值的解决方案,所以这不是太相关了这个问题...什么是新的/与此有关正在处理JArray对象的JSON ...]

Each item in the List collection would be a JObject; each of these objects would have a synthetic "parent" string property that would point back to the branch under which that JObject appears in the original JSON (my examples below explain what I mean by "parent"). [The previous question correctly came up with a solution for this "parent" value, so that's not too relevant to this question... What's new/relevant here is dealing with JArray objects in the JSON...]

关键是,我想每个列表项的对象仅包含对象的字符串值属性。例如,分支1具有字符串属性Prop1A,1B和1C。因此,我想查询[0]包含:

The key is that I want each List item object to contain only the string-value properties for the object. For example, Branch1 has string properties Prop1A, 1B, and 1C. Thus, I would want query[0] to contain:

{"Prop1A":"1A","Prop1B":"1B","Prop1C":"1C", Parent:""}

接下来,我想查询[2]包含Branch2的字符串值属性:

Next, I would want query[2] to contain the string-value properties for Branch2:

{"Prop2A":"2A","Prop2B":"2B","Prop2C":"2C", Parent:"Branch1"}

接下来,我想查询[2]包含仅店3字符串的属性,但因为店3是对象的数组,我想该数组查询一起结束了[2]:

Next, I would want query[2] to contain the string properties for only Branch3, but because Branch3 is an array of objects, I would want that array to end up together in query[2]:

[
 {"Prop3A": "3A","Prop3B": "3B","Prop3C": "3C"},
 {"Prop3D": "3D","Prop3E": "3E","Prop3F": "3F"},
 {"Prop3G": "3G","Prop3H": "3H","Prop3I": "3I"}
]

注意,这个分公司还没有一个参考它的父母......我很乐意让查询[2],看起来像上面的数组。 (我打算使用DBC的逻辑来一个父属性添加到每个数组元素或想出一个办法来创建一个包含数组引用了家长只有一次新JObject):

Notice that this Branch doesn't yet have a reference to its "Parent"... I'd be happy getting an array in query[2] that looks like the above. (I plan to use dbc's logic to add a "Parent" property to each array element or figure out a way to create a new JObject that contains the array and cites the Parent only once):

[{"Prop3A": "3A","Prop3B": "3B","Prop3C": "3C","Parent":"Branch2"},
 {"Prop3D": "3D","Prop3E": "3E","Prop3F": "3F","Parent":"Branch2"},
 {"Prop3G": "3G","Prop3H": "3H","Prop3I": "3I","Parent":"Branch2"}
]

所以,你可以看到:
*我想任何JSON的分支,是不是只有它的字符串属性和一起被插入在查询结果新JObject数组引用其父分支。
*我想任何JSON分支,是一个数组插入在只有它的字符串性能和其父分支的参考沿查询结果新JObject数组。

So, as you can see: * I would want any JSON branch that is not an array to be inserted as a new JObject in the query result along with only its string properties and a reference to its parent branch. * I would want any JSON branch that is an array to be inserted as a new JObject array in the query result along with only its string properties and a reference to its parent branch.

我对我自己解决这个麻烦了,首先要弄清楚如何创建办,如果myJsonObject是JArrayLinq中的条件和分配只是字符串属性属性时,该分支是不是一个数组,当它是一个迭代JArray数组的元素。我怀疑我需要以某种方式利用? :三元的表情,但我不知道到底该怎么做。

The trouble I had solving this on my own has to do with figuring out how to create the "if myJsonObject is JArray" condition in Linq and assigning just the string-property properties when the branch is not an array and iterating the array's elements when it is a JArray. I suspect I need to somehow leverage the ? : ternary expression, but I don't exactly know how to do that.

从以前的查询有问题就在这里:

The query from the previous question is here:

var query3 = from o in root.DescendantsAndSelf().OfType<JObject>()      // Find objects
             let l = o.Properties().Where(p => p.Value is JValue)       // Select their primitive properties
             where l.Any()                                              // Skip objects with no properties
             // Add synthetic "Parent" property
             let l2 = l.Concat(new[] { new JProperty("Parent", o.Ancestors().OfType<JProperty>().Skip(1).Select(a => a.Name).FirstOrDefault() ?? "") })
             select new JObject(l2);                                    // And return a JObject.

var list3 = query3.ToList();

这代码不处理上述方式阵列。

That code doesn't handle arrays in the way described above.

感谢您的阅读!

推荐答案

一个合适的,更普遍的回答这个问题,有效地出现在我的相关岗位:
从分层JSON 挑出​​简单性质

A suitable and more general answer to this question effectively appears in my related post: Picking Out Simple Properties from Hierarchical JSON

这篇关于从分层JSON第二部分挑选出简单性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆