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

C# C# Streams and Data Processing Serialization Deserializing with Json.NET

Calvin Secrest
Calvin Secrest
24,815 Points

Did you instantiate a new JsonSerializer? "Please help with this syntax"

Inside the using block, instantiate a new JsonSerializer named serializer. Call the Deserialize method on the serializer object. The Deserialize method should take type parameter of List<WeatherForecast> and take the jsonReader object as a method parameter. Assign the result of calling the Deserialize method to the weatherForecasts variable.

Program.cs
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Treehouse.CodeChallenges
{
    public class Program
    {        
        public static List<WeatherForecast>DeserializeWeather(string fileName  )
        {
            weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader);
            var serializer = new JsonSerializer ();
            using (StreamReader reader = new StreamReader(fileName))
            using (var jsonReader = new JsonTextReader(reader))
            {                
            }
            var weatherForecasts = new List<WeatherForecast>();
            return weatherForecasts;
        }
        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;
        }
    }
}
WeatherForecast.cs
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
    }
}

2 Answers

Roman Fincher
Roman Fincher
18,267 Points

You have all the pieces, just in the wrong order.

  • You need to instantiate your empty list weatherForecasts first, so you have a place to put the deserialized data.
  • Then you have your two using statements.
  • Then inside the block of those using statements, you instantiate a JsonSerializer
  • Then (also inside the using block) you run your Deserialize method and assign it to weatherForecasts

let me know if your still struggling after trying to work with that, and I'll include some code to help. You're very close though. Don't forget to use preview to see your compiler errors! Those should be helpful.

Calvin Secrest
Calvin Secrest
24,815 Points

Hey, thanks for giving me assistance but I am still having issues. The error I'm getting "Did you instantiate a new JsonSerializer?"

Roman Fincher
Roman Fincher
18,267 Points

As described. Note you're probably getting the error you have because of the space between JsonSerializer and the () in

var serializer = new JsonSerializer();

Passing code below, but I would encourage you not to just copy and paste, and make sure you understand why the order of these statements is the way that it is.

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