<small id='sGED9'></small><noframes id='sGED9'>

    <tfoot id='sGED9'></tfoot>
  1. <legend id='sGED9'><style id='sGED9'><dir id='sGED9'><q id='sGED9'></q></dir></style></legend>

    1. <i id='sGED9'><tr id='sGED9'><dt id='sGED9'><q id='sGED9'><span id='sGED9'><b id='sGED9'><form id='sGED9'><ins id='sGED9'></ins><ul id='sGED9'></ul><sub id='sGED9'></sub></form><legend id='sGED9'></legend><bdo id='sGED9'><pre id='sGED9'><center id='sGED9'></center></pre></bdo></b><th id='sGED9'></th></span></q></dt></tr></i><div id='sGED9'><tfoot id='sGED9'></tfoot><dl id='sGED9'><fieldset id='sGED9'></fieldset></dl></div>
        <bdo id='sGED9'></bdo><ul id='sGED9'></ul>
    2. 如何从 JSON 字符串中获取深度嵌套的属性?

      时间:2023-08-24

      <small id='NKPrU'></small><noframes id='NKPrU'>

        <tbody id='NKPrU'></tbody>

        <tfoot id='NKPrU'></tfoot>

            <bdo id='NKPrU'></bdo><ul id='NKPrU'></ul>

              1. <i id='NKPrU'><tr id='NKPrU'><dt id='NKPrU'><q id='NKPrU'><span id='NKPrU'><b id='NKPrU'><form id='NKPrU'><ins id='NKPrU'></ins><ul id='NKPrU'></ul><sub id='NKPrU'></sub></form><legend id='NKPrU'></legend><bdo id='NKPrU'><pre id='NKPrU'><center id='NKPrU'></center></pre></bdo></b><th id='NKPrU'></th></span></q></dt></tr></i><div id='NKPrU'><tfoot id='NKPrU'></tfoot><dl id='NKPrU'><fieldset id='NKPrU'></fieldset></dl></div>
                <legend id='NKPrU'><style id='NKPrU'><dir id='NKPrU'><q id='NKPrU'></q></dir></style></legend>
                本文介绍了如何从 JSON 字符串中获取深度嵌套的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何使用 JSON.NET 从 JSON 中获取myThings"数组,如下所示?

                How can I get the "myThings" array out of JSON that looks like the following using JSON.NET?

                示例 1:

                {
                    "z": {
                        "a": 1,
                        "b": 2
                    },
                    "something": {
                        "y": [1, 2],
                        "somethingElse": {
                            "1234": {
                                "foo": "bar",
                                "myThings": [{
                                    "name": "bob",
                                    "age": 3
                                }, {
                                    "name": "bob",
                                    "age": 3
                                }]
                            }
                        }
                    }
                }
                

                示例 2:

                {
                    "z": {
                        "a": 1,
                        "b": 2
                    },
                    "something": {
                        "y": [1, 2],
                        "somethingElse": {
                            "7890": {
                                "foo": "bar"
                            }
                        }
                    }
                }
                

                我遇到困难的几件事:

                • 其中一个属性名称是一个不可预测的数字(1234"和7890")
                • 有时myThings"数组不存在 - 在这种情况下,我想要的是 null 或空数组/集合

                另一个对你有帮助的考虑:我确实有一个静态类来表示 myThings 数组中的东西,所以我理想的返回值是 IEnumerable<MyThing>

                Another consideration in case it helps you: I do have a static class that represents the things inside of the myThings array, so my ideal return value would be IEnumerable<MyThing>

                我的第一次尝试是使用 JsonConvert.DeserializeObject<dynamic>(json) 但我不知道如何处理我上面提到的问题.最后,我不需要整个 JSON 字符串的数据,只需要名为myThings"的内部数组.

                My first attempt was to use JsonConvert.DeserializeObject<dynamic>(json) but I don't know how to handle the issues I mentioned above. In the end, I don't need the entire JSON string's worth of data, just that inside array named "myThings".

                推荐答案

                可以使用JToken.SelectTokens() 用于此目的.它允许使用通配符查询 JSON 并使用 JSONPath 语法进行递归搜索:

                You can use JToken.SelectTokens() for this purpose. It allows for querying JSON using wildcards and recursive searches using the JSONPath syntax:

                var root = JToken.Parse(json);
                var myThings = root.SelectTokens("..myThings[*]").ToList();
                

                这里".."递归下降操作符,"myThings[*]"表示返回属性的所有数组项myThings".

                Here ".." is the recursive descent operator and "myThings[*]" means to return all array items of the property "myThings".

                原型小提琴.

                如果 "myThings[*]" 的数组条目对应一些 POCO MyThing,则可以使用 JToken.ToObject() 查询后反序列化:

                If the array entries of "myThings[*]" correspond to some POCO MyThing, you can use JToken.ToObject<T>() to deserialize them after querying:

                    var myThings = root.SelectTokens("..myThings[*]").Select(t => t.ToObject<MyThing>()).ToList();
                

                这篇关于如何从 JSON 字符串中获取深度嵌套的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何仅将缩进序列化应用于某些属性? 下一篇:序列化时子元素的 Newtonsoft 内联格式

                相关文章

                最新文章

                • <bdo id='jMzWZ'></bdo><ul id='jMzWZ'></ul>
                <tfoot id='jMzWZ'></tfoot>
                1. <i id='jMzWZ'><tr id='jMzWZ'><dt id='jMzWZ'><q id='jMzWZ'><span id='jMzWZ'><b id='jMzWZ'><form id='jMzWZ'><ins id='jMzWZ'></ins><ul id='jMzWZ'></ul><sub id='jMzWZ'></sub></form><legend id='jMzWZ'></legend><bdo id='jMzWZ'><pre id='jMzWZ'><center id='jMzWZ'></center></pre></bdo></b><th id='jMzWZ'></th></span></q></dt></tr></i><div id='jMzWZ'><tfoot id='jMzWZ'></tfoot><dl id='jMzWZ'><fieldset id='jMzWZ'></fieldset></dl></div>

                2. <small id='jMzWZ'></small><noframes id='jMzWZ'>

                  <legend id='jMzWZ'><style id='jMzWZ'><dir id='jMzWZ'><q id='jMzWZ'></q></dir></style></legend>