使用Json.NET动态



我正试图使用Json.NET将一些Json反序列化为Dynamic。下面是在使用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属性是一个数组(由其周围的[]表示):

d.weather[0].main;

相关内容

  • 没有找到相关文章

最新更新