【问题标题】:Dynamic JSON querying JSON.NET动态 JSON 查询 JSON.NET
【发布时间】:2017-03-06 16:48:13
【问题描述】:

我有这段 JSON:

{ 
    "grid": 4, 
    "rows": [ 
        { 
            "label": "Full Width", 
            "name": "FullWidth", 
            "areas": [ 
                { 
                    "grid": 16, 
                    "hasConfig": false, 
                    "controls": [ 
                        { 
                            "value": "Test", 
                            "editor": { 
                                "name": "Headline", 
                                "alias": "headline", 
                                "view": "textstring", 
                                "render": null, 
                                "icon": "icon-coin", 
                                "config": { 
                                    "style": "font-size: 36px; line-height: 45px; font-weight: bold", 
                                    "markup": "<h1>#value#</h1>" 
                                } 
                            }, 
                            "active": false 
                        }, 
                        { 
                            "value": "Test", 
                            "editor": { 
                                "name": "Headline", 
                                "alias": "headline", 
                                "view": "textstring", 
                                "render": null, 
                                "icon": "icon-coin", 
                                "config": { 
                                    "style": "font-size: 36px; line-height: 45px; font-weight: bold", 
                                    "markup": "<h1>#value#</h1>" 
                                    } 
                            }, 
                            "active": false 
                        } 
                    ], 
                    "active": false 
                } 
            ], 
            "hasConfig": false, 
            "id": "e0f6ab79-f562-b86b-f32b-d5520173f0cb", 
            "active": false 
        } 
    ] 
}

我想知道是否有任何行(可能是多行)有任何控件(计数 > 0)。我不需要知道任何字段中包含什么,只要任何行的控制计数 > 0 - 所以只是对或错。我一直在尝试使用 LINQ 查询来实现这一点,但速度并不快!

这个 JSON 是动态的,所以我不能反序列化它。任何帮助将不胜感激! :)

【问题讨论】:

标签: c# json linq json.net


【解决方案1】:

最终使用@dbc 提供的链接 - 这是我的代码:

JObject sideBar = JObject.Parse(sidebarContentCol.ToString());

IEnumerable<JToken> sidebarControls = sideBar.SelectTokens("$.rows[*].areas[*].controls[*]");

var controls = (bool)(sidebarControls.Count() == 0);

【讨论】:

  • 您可以考虑使用.Any() 而不是.Count() - count 将强制迭代整个集合,Any 将在找到第一个后立即返回。
【解决方案2】:

你可以这样做:

string json = @"{ 
    'grid': 4, 
    'rows': [ 
        { 
            'label': 'Full Width', 
            'name': 'FullWidth', 
            'areas': [ 
                { 
                    'grid': 16, 
                    'hasConfig': false, 
                     'controls': [ 
                        { 
                            'value': 'Test', 
                            'editor': { 
                                'name': 'Headline', 
                                'alias': 'headline', 
                                'view': 'textstring', 
                                'render': null, 
                                'icon': 'icon-coin', 
                                'config': { 
                                    'style': 'font-size: 36px; line-height: 45px; font-weight: bold', 
                                    'markup': '<h1>#value#</h1>' 
                                } 
                            }, 
                            'active': false 
                        }, 
                        { 
                            'value': 'Test', 
                            'editor': { 
                                'name': 'Headline', 
                                'alias': 'headline', 
                                'view': 'textstring', 
                                'render': null, 
                                'icon': 'icon-coin', 
                                'config': { 
                                    'style': 'font-size: 36px; line-height: 45px; font-weight: bold', 
                                    'markup': '<h1>#value#</h1>' 
                                    } 
                            }, 
                            'active': false 
                        } 
                    ],
                    'active': false 
                } 
            ], 
            'hasConfig': false, 
            'id': 'e0f6ab79-f562-b86b-f32b-d5520173f0cb', 
            'active': false 
        } 
    ] 
}";

var jobject = JObject.Parse(json);

var test = jobject
    .Descendants()
    .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "controls" 
            && t.Ancestors()
                .Any(a => a.Type == JTokenType.Property && ((JProperty)a).Name == "rows"))
    .Any();

将其解析为JObject,查找名为"controls"Descendant,其父(祖先)名为"rows"test 如果是,则为真,否则为假。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多