Json文件序列化读取

Json文件

[
	{
		"name":"清华大学",
		"location":"北京",
		"grade":"1"
	},
	{
		"name":"北京大学",
		"location":"北京",
		"grade":"2"
	}
]

安装包

Json文件序列化读取_第1张图片

Json文件序列化读取_第2张图片

代码

Program.cs

namespace ConsoleApp6SerializeJson
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var lns = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "School.json"));
            List schools = JsonConvert.DeserializeObject>(lns);
            foreach (School school in schools) 
            {
                Console.WriteLine(school.grade);
                Console.WriteLine(school.name);
                Console.WriteLine(school.location);
            }
            Console.ReadKey();
        }
    }
}

School.cs

namespace ConsoleApp6SerializeJson
{
    internal class School
    {
        public string name { get; set; }
        public string location { get; set; }
        public int grade { get; set; }
    }
}

结果

Json文件序列化读取_第3张图片

你可能感兴趣的:(C#,json)