Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![scott mathison](https://secure.gravatar.com/avatar/511b94aa96d701474519be7659c05afa?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
scott mathison
Courses Plus Student 1,556 PointsIn the Program class, add a public static method named DeserializeWeather that returns a List<WeatherForecast> and takes
In the Program class, add a public static method named DeserializeWeather that returns a List<WeatherForecast> and takes a string fileName parameter. Create a new List<WeatherForecast> variable named weatherForecasts and return it.
Please help
using System;
using System.IO;
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class Program
{
public static void Main(string[] arg)
{
}
public static string DeserializeWeather(string fileName)
{
var weatherForecasts = new List<WeatherForecast>();
return weatherForecasts;
}
public static WeatherForecast ParseWeatherForecast(string[] values)
{
var weatherForecast = new WeatherForecast();
weatherForecast.WeatherStationId = values[0];
DateTime timeOfDay;
if (DateTime.TryParse(values[1], out timeOfDay))
{
weatherForecast.TimeOfDay = timeOfDay;
}
Condition condition;
if (Enum.TryParse(values[2], out condition))
{
weatherForecast.Condition = condition;
}
int temperature;
if (int.TryParse(values[3], out temperature))
{
weatherForecast.Temperature = temperature;
}
double precipitation;
if (double.TryParse(values[4], out precipitation))
{
weatherForecast.PrecipitationChance = precipitation;
}
if (double.TryParse(values[5], out precipitation))
{
weatherForecast.PrecipitationAmount = precipitation;
}
return weatherForecast;
}
}
}
using System;
/* Sample JSON
[
{
"weather_station_id": "HGKL8Q",
"time_of_day": "06/11/2016 0:00",
"condition": "Rain",
"temperature": 53,
"precipitation_chance": 0.3,
"precipitation_amount": 0.03
},
{
"weather_station_id": "HGKL8Q",
"time_of_day": "06/11/2016 6:00",
"condition": "Cloudy",
"temperature": 56,
"precipitation_chance": 0.08,
"precipitation_amount": 0.01
},
{
"weather_station_id": "HGKL8Q",
"time_of_day": "06/11/2016 12:00",
"condition": "PartlyCloudy",
"temperature": 70,
"precipitation_chance": 0,
"precipitation_amount": 0
},
{
"weather_station_id": "HGKL8Q",
"time_of_day": "06/11/2016 18:00",
"condition": "Sunny",
"temperature": 76,
"precipitation_chance": 0,
"precipitation_amount": 0
},
{
"weather_station_id": "HGKL8Q",
"time_of_day": "06/11/2016 19:00",
"condition": "Clear",
"temperature": 74,
"precipitation_chance": 0,
"precipitation_amount": 0
}
]
*/
namespace Treehouse.CodeChallenges
{
public class WeatherForecast
{
public string WeatherStationId { get; set; }
public DateTime TimeOfDay { get; set; }
public Condition Condition { get; set; }
public int Temperature { get; set; }
public double PrecipitationChance { get; set; }
public double PrecipitationAmount { get; set; }
}
public enum Condition
{
Rain,
Cloudy,
PartlyCloudy,
PartlySunny,
Sunny,
Clear
}
}
3 Answers
![D Elis](https://secure.gravatar.com/avatar/62b25ec6d9b26a7ab09e47cef2b8f13e?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
D Elis
13,571 PointsWorking code: using System; using System.IO; using System.Collections.Generic; using Newtonsoft.Json;
namespace Treehouse.CodeChallenges { public class Program { public static void Main(string[] arg) { }
public static WeatherForecast ParseWeatherForecast(string[] values)
{
var weatherForecast = new WeatherForecast();
weatherForecast.WeatherStationId = values[0];
DateTime timeOfDay;
if (DateTime.TryParse(values[1], out timeOfDay))
{
weatherForecast.TimeOfDay = timeOfDay;
}
Condition condition;
if (Enum.TryParse(values[2], out condition))
{
weatherForecast.Condition = condition;
}
int temperature;
if (int.TryParse(values[3], out temperature))
{
weatherForecast.Temperature = temperature;
}
double precipitation;
if (double.TryParse(values[4], out precipitation))
{
weatherForecast.PrecipitationChance = precipitation;
}
if (double.TryParse(values[5], out precipitation))
{
weatherForecast.PrecipitationAmount = precipitation;
}
return weatherForecast;
}
public static List<WeatherForecast> DeserializeWeather(string fileName)
{
var weatherForecasts = new List<WeatherForecast>();
using (var reader = new StreamReader(fileName))
using (var jsonReader = new JsonTextReader(reader))
{
var serializer = new JsonSerializer();
weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader);
}
return weatherForecasts;
}
}
}
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe instructions say that the method *"returns a List<WeatherForecast>
", which this code does inside the method body; but the declaration indicates the return value to be a string
instead.
Just change the return type in the declaration and you'll pass task 1:
public static List<WeatherForecast> DeserializeWeather(string fileName)
![Deji Adeyemo](https://secure.gravatar.com/avatar/4ee541b2babb1eb46edfad7f264366f6?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Deji Adeyemo
5,146 Pointspublic static List<WeatherForecast> DeserializeWeather (string fileName)
{
var weatherForecasts = new List<WeatherForecast>();
return weatherForecasts;
}