1

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";
        }
2
  • Btw. You could make your properties public string Name { get; set; }? Commented Oct 19, 2019 at 5:35
  • Yes, I think I can but other parts of the project I have been setting the get/set like I have shown so I don't feel like switching now. Already tried switching and testing it with auto-implementation and nothing new happened. Commented Oct 19, 2019 at 8:22

2 Answers 2

1

1

I don't know this deserialiser well but

deser = new DataContractJsonSerializer(typeof(List<T>));

Should likely be something like

deser = new DataContractJsonSerializer(typeof(CourseWork));

2

  [DataMember(Name = "assignmentname")]
        public List<Assignment> Assignment

quite likely should be

  [DataMember(Name = "assignments")]
        public List<Assignment> Assignment
        {
Sign up to request clarification or add additional context in comments.

1 Comment

Your where kinda right, but there was a little more missing then just that. My solution shows exactly what I needed to add. But yes I needed to make deser = new DataContractJsonSerializer(typeof(List<T>)); Into this deser = new DataContractJsonSerializer(typeof(T)); And adjust other parts in the function to just take in T, and I did need to fix Datamembers and DataContracts, Thank you. `
1

What I did not take into consideration is that CourseWork is not a list, it just has lists inside it as private member variables. To fix my problem I had to change my List<T> ReadJsonFile1<T>(List<T> obj) function back to what I had it earlier in the project T ReadJsonFile1<T>(T obj) Basically just taking all the Lists out and making them normal type of T. I also needed to add DataContract Names to ALL my classes. My DataMembers were fine but since this is a Json Array I needed to add the DataContract above every class like in the Assignment class and also the CourseWork Class, just need to make sure you give your DataContract the correct name.

SOLUTION

List<T> ReadJsonFile1<T>(List<T> obj)
 [DataContract(Name = "coursename")]
  public class CourseWork
  {
[DataContract(Name = "assignments")]
    public class Assignment
     {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.