json example

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;

using Newtonsoft.Json;
					
public class Program
{
	public static void Main()
	{
		
            string json = @"{
                   ""Students"":[
                      {
                         ""Name"":""Mr. X"",
                         ""Class"":""Ten"",
                         ""Roll"":5,
                         ""Subject"": {
                            ""Name"":""Math"",
                            ""Marks"":90
                         }
                      },
                      {
                         ""Name"" : ""Mr. Y"",
                         ""Class"":""Ten"",
                         ""Roll"":7,
                         ""Subject"": {
                            ""Name"":""Math"",
                            ""Marks"":80
                         }
                      }
                   ]
                }";


        Student_Collection students = new Student_Collection();





          var settings = new JsonSerializerSettings
            {
              NullValueHandling = NullValueHandling.Ignore,
              MissingMemberHandling = MissingMemberHandling.Ignore
            };

        // this the line where I am getting the error 
        students = JsonConvert.DeserializeObject<Student_Collection>(json, settings);

        foreach (var item in students.students)
        {
            Console.WriteLine("{0}", item.Name);
        }

        
	}
	
    public class Subject
    {
        public string Name { get; set; }
        public string Marks { get; set; }
    }

    public class Student
    {
        public string Name { get; set; }
        public string Class { get; set; }
        public string Roll { get; set; }
        public Subject Subject { get; set; }
    }

    public class Student_Collection
    {
        public List<Student> students { get; set; }
    }	
}
;