C#和RestSharp的JSON解析问题

iyr7buue  于 2023-08-08  发布在  C#
关注(0)|答案(2)|浏览(148)

解析如下JSON:

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

字符串
我可以通过以下代码访问元素:
responseDeserializeObject.result[0].EventType.events.Local.Events[“530857”].Venue
但是,以下C#代码不起作用:

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);
}


任何帮助都将受到高度赞赏。

eivnm1vs

eivnm1vs1#

我认为你的Events是一个字典,所以你需要在foreach循环中获取KeyValuePair并访问它的Value属性。并且还更改了作用域变量名event,它将无法编译,event是保留字。

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);
}

字符串

bihw5rsg

bihw5rsg2#

你完成这个剧本了吗?

相关问题