• <legend id='wa1Bh'><style id='wa1Bh'><dir id='wa1Bh'><q id='wa1Bh'></q></dir></style></legend>

  • <small id='wa1Bh'></small><noframes id='wa1Bh'>

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

        事先不知道密钥的情况下,如何在 C# 中解析 JS

        时间:2023-08-22
          <i id='UdUCv'><tr id='UdUCv'><dt id='UdUCv'><q id='UdUCv'><span id='UdUCv'><b id='UdUCv'><form id='UdUCv'><ins id='UdUCv'></ins><ul id='UdUCv'></ul><sub id='UdUCv'></sub></form><legend id='UdUCv'></legend><bdo id='UdUCv'><pre id='UdUCv'><center id='UdUCv'></center></pre></bdo></b><th id='UdUCv'></th></span></q></dt></tr></i><div id='UdUCv'><tfoot id='UdUCv'></tfoot><dl id='UdUCv'><fieldset id='UdUCv'></fieldset></dl></div>
          <legend id='UdUCv'><style id='UdUCv'><dir id='UdUCv'><q id='UdUCv'></q></dir></style></legend>

        • <small id='UdUCv'></small><noframes id='UdUCv'>

            <tfoot id='UdUCv'></tfoot>
                <bdo id='UdUCv'></bdo><ul id='UdUCv'></ul>

                    <tbody id='UdUCv'></tbody>
                  本文介绍了事先不知道密钥的情况下,如何在 C# 中解析 JSON 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一些如下所示的 JSON 数据:

                  I have some JSON data that looks like this:

                  {
                    "910719": {
                      "id": 910719,
                      "type": "asdf",
                      "ref_id": 7568
                    },
                    "910721": {
                      "id": 910721,
                      "type": "asdf",
                      "ref_id": 7568
                    },
                    "910723": {
                      "id": 910723,
                      "type": "asdf",
                      "ref_id": 7568
                    }
                  }
                  

                  如何使用 JSON.net 解析它?我可以先这样做:

                  How can I parse this using JSON.net? I can first do this:

                  JObject jFoo = JObject.Parse(data);
                  

                  我需要能够遍历此列表中的每个对象.我希望能够做这样的事情:

                  I need to be able to iterate over each object in this list. I would like to be able to do something like this:

                  foreach (string ref_id in (string)jFoo["ref_id"]) {...}
                  

                  foreach (JToken t in jFoo.Descendants())
                  {
                      Console.WriteLine((string)t["ref_id"]);
                  }
                  

                  但这当然行不通.如果您在编写代码时知道密钥,那么所有示例都非常有用.当您事先不知道密钥时,它就会崩溃.

                  but of course that doesn't work. All the examples work great if you know the key while writing your code. It breaks down when you don't know the key in advance.

                  推荐答案

                  可行;这可行,但并不优雅.我相信有更好的方法.

                  It's doable; this works but it's not elegant. I'm sure there's a better way.

                  var o = JObject.Parse(yourJsonString);
                  
                  foreach (JToken child in o.Children())
                  {
                      foreach (JToken grandChild in child)
                      {
                          foreach (JToken grandGrandChild in grandChild)
                          {
                              var property = grandGrandChild as JProperty;
                  
                              if (property != null)
                              {
                                  Console.WriteLine(property.Name + ":" + property.Value);
                              }
                          }
                      }
                  }
                  

                  打印:

                  id:910719
                  type:asdf
                  ref_id:7568
                  id:910721
                  type:asdf
                  ref_id:7568
                  id:910723
                  type:asdf
                  ref_id:7568

                  这篇关于事先不知道密钥的情况下,如何在 C# 中解析 JSON 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JSON.NET 序列化 JObject 而忽略空属性 下一篇:序列化同一类中的多个 DateTime 属性,每个属性使

                  相关文章

                  最新文章

                    <bdo id='1CYBe'></bdo><ul id='1CYBe'></ul>
                  1. <small id='1CYBe'></small><noframes id='1CYBe'>

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

                      <legend id='1CYBe'><style id='1CYBe'><dir id='1CYBe'><q id='1CYBe'></q></dir></style></legend>