简体   繁体   中英

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. 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 .

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. 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. 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.

            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. You can see what I mean by discounts being a parent object with child values such as amountSaved. You can also see that it has a value of 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.

在此处输入图像描述

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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