2015-07-05 52 views
0

我試圖從openweathermap解析7天預測。我在foreach (JObject array in JsonRequest["main"]["wind"]["name"]["weather"])行中發現了null參考,但我不明白爲什麼。我需要填寫我的課程,並顯示預計7天。JSON解析使用JSON.NET窗口電話

string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric"; 
     dynamic results = await DataService.getDataFromService(queryString).ConfigureAwait(false); 

     MyWeather wetherobject = new MyWeather(); 
     PanoramaItemObject ItemObject = new PanoramaItemObject(); 

     var JsonRequest = JObject.Parse(results); 

     foreach (JObject array in JsonRequest["main"]["wind"]["name"]["weather"]) 
     { 
      JObject obj = JObject.Parse(array.ToString()); 
      wetherobject.Temperature=(string)obj["temp"]; 
      wetherobject.Humidity=(string)obj["humidity"]; 
      wetherobject.Temperature_max=(string)obj["temp_max"]; 
      wetherobject.Temperature_min=(string)obj["temp_min"]; 
      wetherobject.Title=(string)obj["main"]; 
      wetherobject.Description=(string)obj["description"]; 
      wetherobject.Wind=(string)obj["speed"]; 
      ItemObject.forecasts.Add(wetherobject); 
     } 

的層次結構如下:

public class Coord 
{ 
    public double lon { get; set; } 
    public double lat { get; set; } 
} 

public class City 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public Coord coord { get; set; } 
    public string country { get; set; } 
    public int population { get; set; } 
} 

public class Main 
{ 
    public double temp { get; set; } 
    public double temp_min { get; set; } 
    public double temp_max { get; set; } 
    public double pressure { get; set; } 
    public double sea_level { get; set; } 
    public double grnd_level { get; set; } 
    public int humidity { get; set; } 
    public double temp_kf { get; set; } 
} 

public class Weather 
{ 
    public int id { get; set; } 
    public string main { get; set; } 
    public string description { get; set; } 
    public string icon { get; set; } 
} 

public class Clouds 
{ 
    public int all { get; set; } 
} 

public class Wind 
{ 
    public double speed { get; set; } 
    public double deg { get; set; } 
} 

public class Sys 
{ 
    public string pod { get; set; } 
} 

public class Rain 
{ 
    public double __invalid_name__3h { get; set; } 
} 

public class List 
{ 
    public int dt { get; set; } 
    public Main main { get; set; } 
    public List<Weather> weather { get; set; } 
    public Clouds clouds { get; set; } 
    public Wind wind { get; set; } 
    public Sys sys { get; set; } 
    public string dt_txt { get; set; } 
    public Rain rain { get; set; } 
} 

public class RootObject 
{ 
    public string cod { get; set; } 
    public string message { get; set; } 
    public City city { get; set; } 
    public int cnt { get; set; } 
    public string model { get; set; } 
    public List<List> list { get; set; } 
} 

我所做的就像是建議。豁免是:'Newtonsoft.Json.Linq.JObject'不包含'list'的定義

string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric"; 

     HttpClient client = new HttpClient(); 
     string results = await client.GetStringAsync(queryString).ConfigureAwait(false); 
     MyWeather wetherobject = new MyWeather(); 
     PanoramaItemObject ItemObject = new PanoramaItemObject(); 

     dynamic JsonRequest = JObject.Parse(results); 

     foreach (var array in JsonRequest.list) 
     { 
      //JObject obj = JObject.Parse(array.ToString()); 
      wetherobject.Temperature=(string)array.main.temp; 
      wetherobject.Humidity=(string)array.main.humidity; 
      wetherobject.Temperature_max=(string)array.main.temp_max; 
      wetherobject.Temperature_min = (string)array.main.temp_min; 
      wetherobject.Title=(string)array.weather.main; 
      wetherobject.Description=(string)array.weather.description; 
      wetherobject.Wind=(string)array.wind.speed; 
      ItemObject.forecasts.Add(wetherobject); 

     } 
+1

您的對象層次結構不像'main.wind.name.weather'。將你的json粘貼到http://json2csharp.com/並查看結果。 (您也可以使用JsonViewer https://jsonviewer.codeplex.com/) – EZI

+0

同意EZI所說的。也可以嘗試發佈到http://jsonformatter.curiousconcept.com/以可視化您的數據。順便說一句,當查詢一系列屬性時,可能會丟失一些中間屬性,我建議使用['SelectTokens'](http://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm),因爲它會返回一個空的枚舉而不是拋出一個空引用異常。 – dbc

回答

0

您的對象層次結構不同。下面的示例應該可以工作

double latitude = 51.5085300; 
double longitude = -0.1257400; 
string queryString = "http://api.openweathermap.org/data/2.1/forecast/city?lat=" + latitude + "&lon=" + longitude + "?units=metric"; 

HttpClient client = new HttpClient(); 
string results = await client.GetStringAsync(queryString).ConfigureAwait(false); 

dynamic jobj = JObject.Parse(results); 
foreach (var item in jobj.list) 
{ 

    Console.WriteLine("{0} TEMP:{1} WIND:{2}", 
     DateTime.ParseExact((string)item.dt_txt, "yyyy-MM-dd HH:mm:ss", null), 
     (item.main.temp - 273.15), 
     item.wind.speed); 
} 
+0

動態JsonRequest = JObject.Parse(結果); foreach(var array in JsonRequest.list) 現在我得到錯誤'Newtonsoft.Json.Linq.JObject'不包含'list'的定義 – Youhy

+0

@Youhy我發佈了一個經過測試的工作代碼。看看你做什麼不同。 – EZI

+0

你可以使用wetherobject.Temperature作爲輸出而不是控制檯編寫你的例程 – Youhy