.NetCore 超简单读取Json配置文件
.NetCore默认的配置文件appsettings.json,标准json格式,阅读方便使用困难,难就难在于读取
自己封装了一个读取类,可以直接读取各种类型节点(字符串,数值,数组类型,对象),非常简单实用,分享给大家
使用代码:
string connectionString= (string)Config.Get("ConnectionString.EgovPlatformReadOnly")
类代码:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using System.IO; using System.Text; using Microsoft.AspNetCore.Http; namespace Common { public class Config { static Config() { File = "appsettings.json"; string str = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\" + File); objx = JsonConvert.DeserializeObject<dynamic>(str); } public static string File { get; set; } public static dynamic objx; public static dynamic Get(string path) { var obj = objx; foreach (string key in path.Split('.')) { obj = obj[key]; } return obj; } } }