C# Newtonsoft-世界天气在线。从JSON检索值时遇到问题

C# Newtonsoft-世界天气在线。从JSON检索值时遇到问题,c#,json,json.net,windows-phone-8.1,C#,Json,Json.net,Windows Phone 8.1,我有以下问题:我成功地从世界天气在线反序列化了JSON private void Parse_Click(object sender, RoutedEventArgs e) { WebClient webClient = new WebClient(); webClient.DownloadStringCompleted += webClient_DownloadStringCompleted; ProgressBarRequest.Visibility = System

我有以下问题:我成功地从世界天气在线反序列化了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\u条件
中获取
temp\u C

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

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

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

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

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

结果不包含根对象数据吗?如果是这样的话,你会做一些像Weather.data.currentCondition[x].temp_c这样的事情吗?在天气中生活的东西的即时窗口中有一个mooch。如果反序列化成功地进入了你的对象图,你应该能够轻松地遍历它。尝试像这样反序列化LocalWeather=JsonConvert.DeserializeObject(e.Result);谢谢,布莱恩!工作得很有魅力!只是一个快速的后续问题,这样我就能正确地理解这一点。在我的课堂上,列表中有列表。例如数据/天气(列表)/天气描述(另一个列表)/值。最后,我想访问
WeatherDesc
Debug.WriteLine()
中的
值。我该怎么做(即,如何从列表中访问一个值,该值本身就在列表中)?谢谢大家!<代码>WeatherDesc weatherlist=weather.data.weather[0].WeatherDesc[0]成功了)
LocalWeather weather = JsonConvert.DeserializeObject<LocalWeather>(e.Result);
Current_Condition currentCondition = weather.data.current_Condition[0];
Debug.WriteLine(currentCondition.temp_C);