Joe
Joe

Reputation: 1157

JsonDocument - Read child object

I have a JSON object that I'm trying to read properties from. Here is a sample:

{"id":"335057af-a156-41c6-a0de-0cdc05856b3d",
"title":"Test 100488-100489 not included",
"code":"QWNCY47Y999",
"start":"2022-08-10T22:00:00.000Z",
"end":"2022-08-11T02:00:00.000Z",
"closeAfter":"2022-08-08T23:59:00.000Z",
"archiveAfter":"2022-11-08T22:00:00.000Z",
"timezone":"America/New_York",
"defaultLocale":"enUS",
"currency":"USD",
"registrationSecurityLevel":"Public",
"status":"Pending",
"eventStatus":"Upcoming",
"planningStatus":"In-Planning",
"testMode":true,
"planners":[{"firstName":"C","lastName":"G","email":"[email protected]"}],
"created":"2021-06-11T09:55:57.667Z",
"lastModified":"2021-07-08T17:21:48.039Z",
"customFields":[
    {"id":"f2cf4864-171f-44fa-919a-248d37e42563",
    "name":"Level of Service",
    "value":["Full Support"],
    "type":"SingleSelect",
    "order":25},
    {"id":"c1539edf-a3e1-4f40-9747-e93f4fedfa5d",
    "name":"Internal/External",
    "value":["Internal","External"],
    "type":"MultiSelect",
    "order":42},
    {"id":"1b525d2a-b3f8-4141-91ae-45de5ee3a2fe",
    "name":"Request ID",
    "value":["MRF000558"],
    "type":"AutoIncrement",
    "order":53}],
"category":{"name":"Conference"},
"_links":{"invitation":{"href":"http://sandbox-www.qwerty.com/d/8nqg6n/1Q"},
"agenda":{"href":"http://sandbox-www.qwerty.com/d/8nqg6n/6X"},
"summary":{"href":"http://sandbox-www.qwerty.com/d/8nqg6n"},
"registration":{"href":"http://sandbox-www.qwerty.com/d/8nqg6n/4W"}},
"virtual":false,
"format":"In-person"}

I can programmatically read string properties, but if one of the properties is another object, I can't read it.

try 
{
        var x1 = content2.RootElement.GetProperty("code");      // it works
        var x2 = content2.RootElement.GetProperty("currency");   // it works
        var x3 = content2.RootElement.GetProperty("start");     // it works

        var x99 = content2.RootElement.GetProperty("summary");      // exception, key not found
        var x999 = x99.GetProperty("href");
}
catch(System.Exception ex)
{
        Console.WriteLine(ex.Message);
}

Ultimately, my goal is to read the summary child object and get property href.

Upvotes: 0

Views: 1555

Answers (2)

Serge
Serge

Reputation: 43969

you have a bug. "summary" goes after "_links"

this works for me

var contentObj = JObject.Parse(content);

var href = contentObj["_links"]["summary"]["href"]; 

output

http://sandbox-www.qwerty.com/d/8nqg6n

for you I guess it could be

var x9 = content2.RootElement.GetProperty("_links");
var x99 = x9.GetProperty("summary");     
 var x999 = x99.GetProperty("href").ToString();
//or just
var x999 = content2.RootElement.GetProperty("_links").GetProperty("summary").GetProperty("href").ToString();

Upvotes: 1

Bruellhusten
Bruellhusten

Reputation: 318

Unless this "but if one of the properties is another object, I can't read it." means that you don't have a well defined format for your JSON file, just create an object for your JSON file and deserialize it. Then you can access it directly via that objects property.

Upvotes: -1

Related Questions