2015-12-02 36 views
0

我想反序列化一些Json到使用Json.NET的动态。按照Deserialize json object into dynamic object using Json.net给出的例子。我已经完成了大部分的工作,但是我在包含内容的Json的一部分中遇到了困难。我已将返回的Json放置在下面顶部的顶部。动态与Json.NET

//{ "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的值,但我认为[]括号正在阻止它。我试过替换它们,但是如果我这样做,它似乎会破坏动态结构。

+0

这些“'[]'围绕内容“是一个数组符号。 –

回答

3

您应该为您想自你的JSON你weather属性来获得weather实例的索引是一个数组(由周围的[]表示):

d.weather[0].main;