0

I am parsing the following JSON:

{
result: [
  {
    EventType: {
    EventTypeDesc: "horse-racing",
    events: {
      Local: {
        Events: {
          530857: {
            Venue: "Northam",
            StateCode: "WA",
            CountryCode: "AUS"
            },
          530858: {
            Venue: "Caulfield",
            StateCode: "VIC",
            CountryCode: "AUS"
            }
          }
        }
      }
    }
  }
  ]
}

I can access the element through following code:

responseDeserializeObject.result[0].EventType.events.Local.Events["530857"].Venue

However, the following C# code doesn't work:

dynamic responseDeserializeObject = HttpGetResponse(endPoint);
foreach (var event in responseDeserializeObject.result[0].EventType.events.Local.Events)
{
  Console.WriteLine(event.Venue);
  Console.WriteLine(event.StateCode);
  Console.WriteLine(event.CountryCode);
}

Any help will be highly appreciated.

2
  • What error do you get? Commented Sep 28, 2017 at 5:24
  • You have a typo in the Console.Writeline(..) Commented Sep 28, 2017 at 5:25

1 Answer 1

2

I think your Events is a dictionary, so you need to get KeyValuePair in the foreach loop and access it's Value property. And also change the scoped variable name event, it will not compile, event is a reserved word.

dynamic responseDeserializeObject = HttpGetResponse(endPoint);
foreach (var ev in responseDeserializeObject.result[0].EventType.events.Local.Events)
{
  var value = ev.Value;

  Console.WriteLine(value.Venue);
  Console.WriteLine(value.StateCode);
  Console.WriteLine(value.CountryCode);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This, plus WriteLine and use another variable name. Surely, var event doesn't compile?
Sorry mechanically get that name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.