Newtonsoft-世界天气在线.从JSON中检索值时出现问题

本文关键字:检索 问题 JSON 世界 在线 Newtonsoft- | 更新日期: 2023-09-27 17:58:59

我遇到以下问题:我成功地从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.

最终,我将尝试从current_condition中获取temp_C值。

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

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

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

以下是各自的类别:

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的正确值?

Newtonsoft-世界天气在线.从JSON中检索值时出现问题

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

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