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 trialshawn khah
15,082 Pointscan't solve challenge 4 0f 4
Inside the using block, call the Serialize method from the serializer object. The Serialize method takes a JsonTextWriter as its first parameter, and a List<WeatherForecast> as its second parameter.
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 void SerializeWeatherForecasts(List<WeatherForecast> weatherForecasts, string fileName)
{
JsonSerializer serializer = new JsonSerializer();
using ( var writer = new StreamWriter(fileName))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
}
}
}
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;
}
}
}
using System;
using Newtonsoft.Json;
namespace Treehouse.CodeChallenges
{
public class WeatherForecast
{
[JsonProperty(PropertyName = "weather_station_id")]
public string WeatherStationId { get; set; }
[JsonProperty(PropertyName = "time_of_day")]
public DateTime TimeOfDay { get; set; }
public Condition Condition { get; set; }
public int Temperature { get; set; }
[JsonProperty(PropertyName = "precipitation_chance")]
public double PrecipitationChance { get; set; }
[JsonProperty(PropertyName = "precipitation_amount")]
public double PrecipitationAmount { get; set; }
}
public enum Condition
{
Rain,
Cloudy,
PartlyCloudy,
PartlySunny,
Sunny,
Clear
}
}
1 Answer
Steven Parker
231,248 PointsIt doesn't look like you've written any code yet for task 4.
Give it your best good-faith try and then ask for help if you have trouble.
If you're totally lost, review the example of creating SerializePlayersToFile starting about time index 1:20 in video Serializing to a File.
Scott George
19,060 PointsScott George
19,060 PointsI believe I've followed the video, but I'm still being asked "Bummer: Did you call the Serialize method on the 'serializer' object? "public static void SerializeWeatherForecasts(List<WeatherForecast> weatherForecasts,string fileName){ var serializer = new JsonSerializer (); using (var writer = new StreamWriter(fileName)) using (var JsonTextWriter = new JsonTextWriter(writer)) { Serialize.serializer(jsontWriter, WeatherForecast); }