1-新建json文件, 设置json文件生成的方式
{ "ClassNo": "1", "ClassDesc": "Asp.net core", "Students": [ { "name": "lili", "age": "11" }, { "name": "xiaofeng", "age": "33" }, { "name": "xiaobao", "age": "66" } ] }
2-解析json文件
using System; using Microsoft.Extensions.Configuration; namespace jsonConfigDemo { class Program { static void Main(string[] args) { var builder = new ConfigurationBuilder() .AddJsonFile("myclass.json"); var config = builder.Build(); Console.WriteLine($"ClassNo: {config["ClassNo"]}"); //下面输出第一个 Console.WriteLine($"name: {config["Students:0:name"]} age:{config["Students:0:age"]}"); //下面输出第二个 Console.WriteLine($"name: {config["Students:1:name"]} age:{config["Students:1:age"]}"); Console.ReadLine(); } } }
结果