新建"appsetting.json"文本
{ "username": "administrator", "level": "high", "permissions": [ "login", "read", "write" ], "limit": { "size": 120 } }
新建控制台应用程序
class Program { static void Main(string[] args) { // 1 string jsonStr = File.ReadAllText("appsetting.json"); var jsonObj = JObject.Parse(jsonStr); string username = jsonObj["username"].ToString(); string level = jsonObj.Value<string>("level"); List<string> permissions = jsonObj.GetValue("permissions").ToObject<List<string>>(); // 2 var jt = jsonObj.SelectToken("limit.size"); var size = jt.Value<int>(); // 3 UserInfo user = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(Encoding.UTF8.GetString(File.ReadAllBytes("appsetting.json"))); Console.WriteLine("Hello World!"); } } public class UserInfo { public string UserName { get; set; } public string Level { get; set; } public List<string> Permissions { get; set; } }