C# 使用Json.NET动态

C# 使用Json.NET动态,c#,json.net,C#,Json.net,我正在尝试使用Json.NET将一些Json反序列化为动态文件。以下是在中给出的示例。我已经了解了其中的大部分内容,但是我对Json的一个部分有困难,其中包括内容周围的[]。我已将返回的Json放在下面顶部的顶部 //{ "coord":{ "lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}], // "base":"cmc sta

我正在尝试使用Json.NET将一些Json反序列化为动态文件。以下是在中给出的示例。我已经了解了其中的大部分内容,但是我对Json的一个部分有困难,其中包括内容周围的[]。我已将返回的Json放在下面顶部的顶部

//{ "coord":{ "lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
//  "base":"cmc stations","main":{"temp":283.582,"pressure":1024.62,"humidity":91,"temp_min":283.582,"temp_max":283.582,
//  "sea_level":1034.82,"grnd_level":1024.62},"wind":{"speed":6.61,"deg":222},"clouds":{"all":92},"dt":1449047315,
//"sys":{"message":0.0052,"country":"GB","sunrise":1449042313,"sunset":1449071662},"id":2643743,"name":"London","cod":200}

var url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";

// Synchronous Consumption
var syncClient = new WebClient();
var content = syncClient.DownloadString(url);

dynamic d = JObject.Parse(content);

//Can now do d.weather

dynamic d2 = d.weather;
//var weatherMain = d.weather.main; // this doesn't work :( -- I think because because the sub-json has more [] around it

dynamic d3 = d.coord;
var lon = d3.lon; //this works

var pink = 2;

我想检索d.weather.main的值,但我认为[]括号阻止了它。我已经尝试过替换它们,但如果我这样做,似乎会破坏动态结构。

您应该提供想要获取的
weather
实例的索引,因为JSON中的
weather
属性是一个数组(由围绕它的
[]
指示):


由于JSON中的
weather
属性是一个数组(由其周围的
[]
指示),因此您应该提供想要获取的
weather
实例的索引:

这些“
[]
围绕内容”是数组表示法。这些“
[]
围绕内容”是数组表示法。
d.weather[0].main;