Question

I have the following issue: I deserialize JSON from World Weather Online successfully

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"));
}

.. where xxxxxxxx is my API key.

Then I successfully deserialize the whole thing

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;
      }
}

I retrieve the following:

{
  "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"
      }
    ]
  }
}

... as seen from my Debug.WriteLine.

Ultimately, I'm trying to get the temp_C value from current_condition.

However, all my efforts so far have been a failure. In particular, the following piece of code:

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

... returns 0 when I perform Debug.WriteLine(weather.temp_C).

Here are the respective classes:

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; }
}

What should I do to get the proper value of temp_C?

Was it helpful?

Solution

It is not working because you are deserializing into the wrong class. The Current_Condition is not at the root of your JSON, but you are treating it as if it were. You need to deserialize into the LocalWeather class instead, then retrieve the data from that.

LocalWeather weather = JsonConvert.DeserializeObject<LocalWeather>(e.Result);
Current_Condition currentCondition = weather.data.current_Condition[0];
Debug.WriteLine(currentCondition.temp_C);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top