简体   繁体   English

"如何从 C# 中的 json 父数组中获取嵌套对象"

[英]How to get a nested object from an json parent array in c#

In a C# Controller.cs how do I get a "child" or "nested" object from a "parent" object.在 C# Controller.cs 中,如何从“父”对象获取“子”或“嵌套”对象。 I am currently able to retrieve the values for objects that are NOT nested, but I also need to be able to grab the values to certain child objects as well.我目前能够检索嵌套对象的值,但我还需要能够获取某些子对象的值。

Please see my currently working code below that actually retrieves the data when the object is NOT nested.请参阅下面我当前工作的代码,该代码在对象嵌套时实际检索数据。 I am able to get the values invoiceNumber, email, and paymentMethod .我能够获得 invoiceNumber、email 和 paymentMethod 的值。

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();                
            var obj = JObject.Parse(result);

            
            string invoiceNumber = (string)obj["invoiceNumber"];
            string email = (string)obj["email"];
            string paymentMethod = (string)obj["paymentMethod"];
            
        }

Now when I attempt to get the data for objects that are nested, I just get retuned NULL.现在,当我尝试获取嵌套对象的数据时,我只是重新调整为 NULL。 These values are NOT actually null as when I am debugging in visual studio JSON Visualizer, they have values but I just do not know how to write the syntax I need in order to grab these that are "child" or "nested" objects.当我在 Visual Studio JSON Visualizer 中调试时,这些值实际上并不为空,它们具有值,但我只是不知道如何编写我需要的语法来获取这些“子”或“嵌套”对象。 So as you can see in the code below I tried to call this value for amountSaved in every way that I could think of, but it just returns NULL each time.因此,正如您在下面的代码中看到的那样,我尝试以我能想到的各种方式调用 amountSaved 的这个值,但它每次都返回 NULL。

            string rate = (string)obj["discounts.[0].amountSaved"];
            string rate2 = (string)obj["discounts.amountSaved"];
            string rate3 = (string)obj["discounts amountSaved"];
            string rate4 = (string)obj["discounts:amountSaved"];
            string rate5 = (string)obj["{discounts}:amountSaved"];
            string rate6 = (string)obj["{discounts}.amountSaved"];
            string rate7 = (string)obj["discounts.{amountSaved}"];
            string rate8 = (string)obj["{discounts.amountSaved}"];

I figured the solution might be something like I call the parent object first which in this case is called discounts and then after a period or colon or space(. :) or something, I enter the name of the child object which in this case is amountSaved.我认为解决方案可能类似于我首先调用父对象,在这种情况下称为折扣,然后在句点或冒号或空格(。:) 或其他东西之后,我输入子对象的名称,在这种情况下是节省的金额。 That did not work.那没有用。

Please see my screenshot below.请看我下面的截图。 This is when I debugging in visual studio and use the JSON Visualizer.这是我在 Visual Studio 中调试并使用 JSON Visualizer 的时候。 You can see what I mean by discounts being a parent object with child values such as amountSaved.您可以看到我所说的折扣是具有子值(例如 amountSaved)的父对象的意思。 You can also see that it has a value of 0.75.您还可以看到它的值为 0.75。 You can can also see what I mean by the other values that are not nested which I am able to grab like shippingMethod.您还可以看到我所说的其他未嵌套的值是什么意思,我可以像 shippingMethod 一样抓取这些值。

在此处输入图像描述

How can I accomplish this?我怎样才能做到这一点? Thank you.谢谢你。

when you use the square brakets you should not put .当你使用方刹车时,你不应该放。 between them.它们之间。 And you can omit square brakets and use dot syntax only if you have deserialized object, not just parsed it只有当你有反序列化对象时,你才能省略方括号并使用点语法,而不仅仅是解析它

 string rate = (string)obj["discounts"][0]["amountSaved"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM