Newtonsoft - 世界天气在线。从JSON检索值时遇到麻烦

时间:2014-04-19 19:05:09

标签: c# json json.net windows-phone-8.1

我有以下问题:我成功地从World Weather Online反序列化JSON

private void Parse_Click(object sender, RoutedEventArgs e)
{
    WebClient webClient = new WebClient();
    webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
    ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
    webClient.DownloadStringAsync(new Uri("http://api.worldweatheronline.com/free/v1/weather.ashx?q=Minsk&format=json&num_of_days=1&key=xxxxxxxxxxxxxxx"));
}

..其中xxxxxxxx是我的API密钥。

然后我成功地反序列化了整个事情

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
      try
      {
      if (!string.IsNullOrEmpty(e.Result))
         {
              var weather = JsonConvert.DeserializeObject(e.Result);
              Debug.WriteLine(weather);
         }
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.ToString());
      }
      finally
      {
         ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
      }
}

我检索以下内容:

{
  "data": {
    "current_condition": [
      {
        "cloudcover": "0",
        "humidity": "51",
        "observation_time": "06:51 PM",
        "precipMM": "0.0",
        "pressure": "1021",
        "temp_C": "15",
        "temp_F": "59",
        "visibility": "10",
        "weatherCode": "113",
        "weatherDesc": [
          {
            "value": "Clear"
          }
        ],
        "weatherIconUrl": [
          {
            "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"
          }
        ],
        "winddir16Point": "NNE",
        "winddirDegree": "30",
        "windspeedKmph": "7",
        "windspeedMiles": "4"
      }
    ],
    "request": [
      {
        "query": "Minsk, Belarus",
        "type": "City"
      }
    ],
    "weather": [
      {
        "date": "2014-04-19",
        "precipMM": "0.0",
        "tempMaxC": "21",
        "tempMaxF": "71",
        "tempMinC": "8",
        "tempMinF": "47",
        "weatherCode": "113",
        "weatherDesc": [
          {
            "value": "Sunny"
          }
        ],
        "weatherIconUrl": [
          {
            "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
          }
        ],
        "winddir16Point": "E",
        "winddirDegree": "80",
        "winddirection": "E",
        "windspeedKmph": "17",
        "windspeedMiles": "10"
      }
    ]
  }
}

...从我的Debug.WriteLine中看到。

最终,我正试图从temp_C获取current_condition值。

然而,到目前为止,我所有的努力都是失败的。特别是以下代码:

Current_Condition weather = JsonConvert.DeserializeObject<Current_Condition>(e.Result);
Debug.WriteLine(weather.temp_C);

...执行0时返回Debug.WriteLine(weather.temp_C)

以下是各自的课程:

public class LocalWeatherInput
{
      public string query { get; set; }
      public string format { get; set; }
      public string extra { get; set; }
      public string num_of_days { get; set; }
      public string date { get; set; }
      public string fx { get; set; }
      public string cc { get; set; }
      public string includelocation { get; set; }
      public string show_comments { get; set; }
      public string callback { get; set; }
}
public class LocalWeather
{
      public Data data;
}
public class Data
{
      public List<Current_Condition> current_Condition;
      public List<Request> request;
      public List<Weather> weather;
}
      public class Current_Condition
{
      public DateTime observation_time { get; set; }
      public DateTime localObsDateTime { get; set; }
      public int temp_C { get; set; }
      public int windspeedMiles { get; set; }
      public int windspeedKmph { get; set; }
      public int winddirDegree { get; set; }
      public string winddir16Point { get; set; }
      public string weatherCode { get; set; }
      public List<WeatherDesc> weatherDesc { get; set; }
      public List<WeatherIconUrl> weatherIconUrl { get; set; }
      public float precipMM { get; set; }
      public float humidity { get; set; }
      public int visibility { get; set; }
      public int pressure { get; set; }
      public int cloudcover { get; set; }
}  
public class Request
{
      public string query { get; set; }
      public string type { get; set; }
}
public class Weather
{
      public DateTime date { get; set; }
      public int tempMaxC { get; set; }
      public int tempMaxF { get; set; }
      public int tempMinC { get; set; }
      public int tempMinF { get; set; }
      public int windspeedMiles { get; set; }
      public int windspeedKmph { get; set; }
      public int winddirDegree { get; set; }
      public string winddir16Point { get; set; }
      public string weatherCode { get; set; }
      public List<WeatherDesc> weatherDesc { get; set; }
      public List<WeatherIconUrl> weatherIconUrl { get; set; }
      public float precipMM { get; set; }
}
public class WeatherDesc
{
      public string value { get; set; }
}

      public class WeatherIconUrl
{
      public string value { get; set; }
}

我该怎么做才能获得正确的temp_C值?

1 个答案:

答案 0 :(得分:3)

它无效,因为您正在反序列化到错误的类中。 Current_Condition不是您JSON的根,但您将其视为原样。您需要反序列化到LocalWeather类,然后从中检索数据。

LocalWeather weather = JsonConvert.DeserializeObject<LocalWeather>(e.Result);
Current_Condition currentCondition = weather.data.current_Condition[0];
Debug.WriteLine(currentCondition.temp_C);