I am trying to deserialize a JSON file but for some reason, I just can't seem to get it to work. Is this the correct way to deserialize a JSON array to a list.
This is the JSON Data I would like to deserialize and put into the coursework object
{
"assignments":[
{
"categoryname":"Exams",
"description":"Test covers C#, classes and other topics",
"name":"Exam 1"
},
{
"categoryname":"Exams",
"description":"Test covers collections and other topics",
"name":"Exam 2"
},
Deserialization Method I am using
List<T> ReadJsonFile1<T>(List<T> obj)
{
Console.Write("Enter Json File Name: ");
string fileName = Console.ReadLine();
FileStream reader = new FileStream(fileName, FileMode.Open, FileAccess.Read);
DataContractJsonSerializer deser;
deser = new DataContractJsonSerializer(typeof(List<T>));
obj = (List<T>)deser.ReadObject(reader);
reader.Close();
return obj;
}
Code I execute in Main
CourseWork cw = new CourseWork();
cw.Assignment = ReadJsonFile1<Assignment>(cw.Assignment);
CourseWork Class
public class CourseWork
{
private List<Assignment> assignments;
[DataMember(Name = "assignmentname")]
public List<Assignment> Assignment
{
get { return assignments; }
set { assignments = value; }
}
public CourseWork()
{
assignments = new List<Assignment>();
}
Assignment Class
private string name;
private string description;
private string categoryName;
//DataContract
[DataMember(Name = "name")]
public string Name
{
get { return name; }
set { name = value; }
}
//DataContract
[DataMember(Name = "description")]
public string Description
{
get { return description; }
set { description = value; }
}
//DataContract
[DataMember(Name = "categoryname")]
public string CategoryName
{
get { return categoryName; }
set { categoryName = value; }
}
public Assignment()
{
name = "defaut";
description = "defaut";
categoryName = "defaut";
}
public string Name { get; set; }?