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

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

Did you instantiate a new JsonSerializer?

What is wrong with my code? I couldnt find out what is the problem, Please Help!

Program.cs
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 List<WeatherForecast>DeserializeWeather(string fileName)
        {            
            var weatherForecasts = new List<WeatherForecast>( );
            var serializer = new JsonSerializer (fileName );

            using (var reader = new StreamReader(fileName))
            using (var jsonReader = new JsonTextReader(reader))
            {
                using (var jsonReader = new JsonTextReader(reader))
                weatherForecasts = serializer.Deserialize<List<WeatherForecast>>(jsonReader);
            }
            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;
        }
    }
}
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

Steven Parker
Steven Parker
174,057 Points

It looks like you're on task 4 but you did not complete task 3 yet.

In particular, you overlooked the instruction to: "Add a using directive for the Newtonsoft.Json namespace. "

Then for task 4, it looks like you missed this instruction: "Inside the using block, instantiate a new JsonSerializer named serializer." Instead, it appears that you copied your line that creates jsonReader and put the copy inside the block. Unintended copy/paste, perhaps?

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

I tried, but is still saying "Did you instantiate a new JsonSerializer?"

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

What is the problem with this code? it is still not working. I already added the Newtonsoft.Json namespace

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