【问题标题】:Newtonsoft - World Weather Online. Troubles with retrieving a value from JSONNewtonsoft - 世界在线天气。从 JSON 中检索值的问题
【发布时间】:2014-04-19 19:05:09
【问题描述】:

我有以下问题:我成功地从 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 值?

【问题讨论】:

  • e.Result 是否包含根对象、数据?如果是这样,你会做类似 Weather.data.currentCondition[x].temp_c 的事情吗?
  • 在即时窗口中畅谈天气中的生活。如果反序列化成功进入您的对象图,您应该能够轻松地遍历它。
  • 啊,又看了一遍。尝试像这样进行反序列化 LocalWeather weather = JsonConvert.DeserializeObject(e.Result);

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢,布赖恩!像魅力一样工作!
  • 只是一个快速的后续问题,所以我得到了正确的答案。在我的课程中,列表中有列表。它的示例是 Data/Weather(list)/WeatherDesc(another list)/value。最终,我想在WeatherDescDebug.WriteLine() 中访问value。我将如何做到这一点(即如何从列表中访问本身位于列表中的值)?谢谢!
  • WeatherDesc weatherlist = weather.data.weather[0].weatherDesc[0]; 成功了)
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多