如何浏览嵌套的JSON对象' n'像c#中的父母和孩子一样的次数

发布于 2025-01-18 10:53:45 字数 3834 浏览 1 评论 0原文

JSON 示例:

我怎样才能获取 Gum 的值,并循环它出现的次数。基本上我想从 json 中获取 Gum 部分并获取该部分中的所有相应值。如果存在另一个口香糖,我应该在保存第一个口香糖的值后在下一次迭代中循环到它。

另外,我应该用一个变量标记第一个 Gum 部分并为其保存一个值。 (作为母体胶)。父口香糖为空的变量将被视为子口香糖。

{
   "Response": {
      "ResponseAttributes": [
         {
            "@Key": "MessagePurpose",
            "#text": "PROD"
         },
         {
            "@Key": "SHIP_TO_COUNTRY",
            "#text": "BR"
         },
         {
            "@Key": "SourcePlan",
            "#text": "LEGACY"
         }
      ],
      "Gums": {
         "Gum": {
            "Level": "1",
            "TranFro": {
               "@type": "3PL",
               "#text": "BFC"
            },
            "TranTo": {
               "@type": "CUSTOMER",
               "#text": "CUST"
            },
            "Trail": "G",
            "FSBD": "2022-02-17T00:00:00",
            "FABD": "2022-02-17T00:00:00",
            "IsNorth": "N",
            "Gum": {
               "Level": "2",
               "TranFro": {
                  "@type": "3PL",
                  "#text": "BMC"
               },
               "TranTo": {
                  "@type": "3PL",
                  "#text": "BFC"
               },
               "Trail": "G",
               "FSBD": "2022-02-17T00:00:00",
               "FABD": "2022-02-17T00:00:00",
               "IsNorth": "N",
               "Gum": {
                  "Level": "3",
                  "TranFro": {
                     "@type": "ODM",
                     "#text": "BR1X"
                  },
                  "TranTo": {
                     "@type": "3PL",
                     "#text": "BMC"
                  },
                  "Trail": "G",
                  "FSBD": "2022-02-17T00:00:00",
                  "FABD": "2022-02-17T00:00:00",
                  "IsNorth": "N",
                  "Parts": [],
                  "Services": [],
                  "Attributes": [
                     {
                        "@Key": "Bear",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "code",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "PHJ",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "NewDir",
                        "#text": "N"
                     },
                     {
                        "@Key": "RotType",
                        "#text": "SINGLE"
                     }
                  ]
               },
               "Parts": [],
               "Services": [],
               "Attributes": [
                  {
                     "@Key": "Bear",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "code",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "PHJ",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "RotType",
                     "#text": "SINGLE"
                  },
                  {
                     "@Key": "NewDir",
                     "#text": "N"
                  }
               ]
            },
            "Parts": [],
            "Services": [],
            "Attributes": [
               {
                  "@Key": "RotType",
                  "#text": "SINGLE"
               },
               {
                  "@Key": "NewDir",
                  "#text": "Y"
               }
            ]
         }
      }
   }
}

我尝试对其进行序列化,但只能导航到第一个 Gum - Level-1。 然后循环就会爆发。 foreach(rpJsonObject.Response.Gums 中的 var Gums) <代码>{ //一颗口香糖后就出来了。我知道,因为我不会进入孩子体内 <代码>}

Example of JSON:

How can I go and get values of Gum, and loop it through as many times it is present. Basically I want to get the Gum section from the json and get all the corresponding values in that section. If another Gum is there I should then loop to it in next iteration after saving the values of the first.

Also, I should mark the first Gum section with a variable and save a value for it. (As Parent Gum). The variables with are having parent gum as empty will be considered as Child.

{
   "Response": {
      "ResponseAttributes": [
         {
            "@Key": "MessagePurpose",
            "#text": "PROD"
         },
         {
            "@Key": "SHIP_TO_COUNTRY",
            "#text": "BR"
         },
         {
            "@Key": "SourcePlan",
            "#text": "LEGACY"
         }
      ],
      "Gums": {
         "Gum": {
            "Level": "1",
            "TranFro": {
               "@type": "3PL",
               "#text": "BFC"
            },
            "TranTo": {
               "@type": "CUSTOMER",
               "#text": "CUST"
            },
            "Trail": "G",
            "FSBD": "2022-02-17T00:00:00",
            "FABD": "2022-02-17T00:00:00",
            "IsNorth": "N",
            "Gum": {
               "Level": "2",
               "TranFro": {
                  "@type": "3PL",
                  "#text": "BMC"
               },
               "TranTo": {
                  "@type": "3PL",
                  "#text": "BFC"
               },
               "Trail": "G",
               "FSBD": "2022-02-17T00:00:00",
               "FABD": "2022-02-17T00:00:00",
               "IsNorth": "N",
               "Gum": {
                  "Level": "3",
                  "TranFro": {
                     "@type": "ODM",
                     "#text": "BR1X"
                  },
                  "TranTo": {
                     "@type": "3PL",
                     "#text": "BMC"
                  },
                  "Trail": "G",
                  "FSBD": "2022-02-17T00:00:00",
                  "FABD": "2022-02-17T00:00:00",
                  "IsNorth": "N",
                  "Parts": [],
                  "Services": [],
                  "Attributes": [
                     {
                        "@Key": "Bear",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "code",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "PHJ",
                        "#text": "BMC"
                     },
                     {
                        "@Key": "NewDir",
                        "#text": "N"
                     },
                     {
                        "@Key": "RotType",
                        "#text": "SINGLE"
                     }
                  ]
               },
               "Parts": [],
               "Services": [],
               "Attributes": [
                  {
                     "@Key": "Bear",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "code",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "PHJ",
                     "#text": "BFC"
                  },
                  {
                     "@Key": "RotType",
                     "#text": "SINGLE"
                  },
                  {
                     "@Key": "NewDir",
                     "#text": "N"
                  }
               ]
            },
            "Parts": [],
            "Services": [],
            "Attributes": [
               {
                  "@Key": "RotType",
                  "#text": "SINGLE"
               },
               {
                  "@Key": "NewDir",
                  "#text": "Y"
               }
            ]
         }
      }
   }
}

I tried serializing it but I get to navigate through the first Gum only - Level-1.
The loops break out then.
foreach (var Gums in rpJsonObject.Response.Gums)
{
//After one Gum it comes out. I know since I'm not going inside the child
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文