Asp.Net MVC 读取json文件
有些系统上面的配置可以做成config里面的appsetting。这里要求写在json文件里面。
首先 添加命名空间
using Newtonsoft.Json; using System.IO; using Newtonsoft.Json.Linq; using System.Web;
下面是我json文件里面的内容 因为西门下面的序号是 变动的就写在配置文件里面也好修改
{ "StatisticalChart": { "0": "/Common/CountReport/DefaultStatis", "68": "/Common/CountReport/CustomerManagerStatis", "98": "/Common/CountReport/FirstExamineStatis", "84": "/Common/CountReport/QualityCheckStatis", "112": "/Common/CountReport/ElectricFinalCheckStatis", "70": "/Common/CountReport/ExecutiveDirectorStatis" }, "NameGetCode":{ "Default": "0", "CustomerManager": "68", "FirstExamine": "98", "QualityCheck": "84", "ElectricFinalCheck": "112", "ExecutiveDirector": "70" } }
下面是读取StatisticalChart节点里面的内容 power权限参数 用户登录之后获得。
/// <summary> /// 通过角色读取配置文件返回地址 /// </summary> /// <param name="power">权限编号</param> /// <returns></returns> public static string GetUrlByPower(string power) { string url = ""; try { //文件路径 string jsonfile = HttpContext.Current.Server.MapPath($"\\App_Data\\config\\StatisticalChartConfig.json"); //打开文件 System.IO.StreamReader file = System.IO.File.OpenText(jsonfile); //加载问卷 JsonTextReader reader = new JsonTextReader(file); //转化为JObject JObject ojb = (JObject)JToken.ReadFrom(reader); //读取StatisticalChart节点 var list = ojb["StatisticalChart"].ToString(); JObject jo = (JObject)JsonConvert.DeserializeObject(list); var dat = jo[power]; if (dat == null) url = ""; else url = dat.ToString(); } catch (Exception e) { url = ""; throw new Exception($"初始化JSON流程配置失败:{e.Message}"); } return url; }
另外一种 中国省市县的json文件
读取的方法(我是通过身份证号码获取信息用到的)
//读取json文件获取代码对应的省市县 string jsonfile =HttpContext.Current.Server.MapPath($"\\App_Data\\Config\\AreasData.json"); StreamReader file = File.OpenText(jsonfile); JsonTextReader reader = new JsonTextReader(file); var obj = JToken.ReadFrom(reader).ToList(); for (int i = 0; i < obj.Count; i++) { JObject jo = (JObject)JsonConvert.DeserializeObject(obj[i].ToString()); if (jo["zoneCode"].ToString() == AreasCodess) { var Data = new AreasCode() { Province = jo["province"].ToString(), ProvinceCode = jo["provinceCode"].ToString(), City = jo["city"].ToString(), CityCode = jo["cityCode"].ToString(), Country = jo["zone"].ToString(), CountryCode = jo["zoneCode"].ToString() }; list.Add(Data); break; } }
AreasCode是个实体类 接收参数的Model,AreasCodess是区域代码。读取json文件代码就这些 全面几步都是一样的 后面要看具体的json文件格式,就跟Ajax一样 返回来数据类型 是后台决定的你才好解析!
获取身份证信息也贴上吧,后面用到又来拿
/// <summary> /// 获取身份证信息 /// </summary> /// <param name="identityCard">身份证号码</param> /// <param name="ageIsTrue">显示年龄是否是真实大小(当前年份减去出生年份 月份 日期都要计算)</param> /// <returns>List->IDCard模型</returns> public static List<IDCard> GetIDCardInfo(string identityCard, bool ageIsTrue = true) { if (string.IsNullOrEmpty(identityCard))//为空返回null return null; else { if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法 return null; } List<IDCard> dCards = new List<IDCard>(); IDCard card = new IDCard(); string strSex = string.Empty; //530326 1994 1201 2521 if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码 { //出身年月日 card.Birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2); strSex = identityCard.Substring(14, 3);// } //530323 670401 001的含义; 530为云南,03为曲靖 ,23是师宗,出生日期为1967年4月1日,顺序号为001。 if (identityCard.Length == 15) { card.Birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2); //性别代码 strSex = identityCard.Substring(12, 3); } card.Age = CalculateAge(card.Birthday, ageIsTrue);//根据生日计算年龄 if (int.Parse(strSex) % 2 == 0)//性别代码为偶数是女性奇数为男性 card.Sex = "女"; else card.Sex = "男"; string AreasCodess = identityCard.Substring(0, 6); List<AreasCode> list = new List<AreasCode>(); try { //读取json文件获取代码对应的省市县 string jsonfile =HttpContext.Current.Server.MapPath($"\\App_Data\\Config\\AreasData.json"); StreamReader file = File.OpenText(jsonfile); JsonTextReader reader = new JsonTextReader(file); var obj = JToken.ReadFrom(reader).ToList(); for (int i = 0; i < obj.Count; i++) { JObject jo = (JObject)JsonConvert.DeserializeObject(obj[i].ToString()); if (jo["zoneCode"].ToString() == AreasCodess) { var Data = new AreasCode() { Province = jo["province"].ToString(), ProvinceCode = jo["provinceCode"].ToString(), City = jo["city"].ToString(), CityCode = jo["cityCode"].ToString(), Country = jo["zone"].ToString(), CountryCode = jo["zoneCode"].ToString() }; list.Add(Data); break; } } if (list.Count > 0) { card.Province = list[0].Province; card.ProvinceCode = list[0].ProvinceCode; card.City = list[0].City; card.CityCode = list[0].CityCode; card.Country = list[0].Country; card.AreasCode = list[0].CountryCode; card.AddressAreas = list[0].Province + list[0].City + list[0].Country; } dCards.Add(card); return dCards; } catch (Exception xx) { dCards[0].Msg = xx.Message; return dCards; } } /// <summary> /// 身份证信息实体类 /// </summary> public class IDCard { /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 性别 /// </summary> public string Sex { get; set; } /// <summary> /// 生日 /// </summary> public string Birthday { get; set; } /// <summary> /// 省份 /// </summary> public string Province { get; set; } /// <summary> /// 市区 /// </summary> public string City { get; set; } /// <summary> /// 县城 /// </summary> public string Country { get; set; } /// <summary> /// 身份证所在区域(省市县) /// </summary> public string AddressAreas { get; set; } /// <summary> /// 区域代码 /// </summary> public string AreasCode { get; set; } /// <summary> /// 省代码 /// </summary> public string ProvinceCode { get; set; } /// <summary> /// 县/区代码 /// </summary> public string CityCode { get; set; } /// <summary> /// 信息 /// </summary> public string Msg { get; set; } /// <summary> /// 状态 /// </summary> public string Status { get; set; } } /// <summary> /// 实体类(省市县跟代码)身份证前6位数字 /// </summary> public class AreasCode { /// <summary> /// 省份 /// </summary> public string Province { get; set; } /// <summary> /// 省份代码 /// </summary> public string ProvinceCode { get; set; } /// <summary> /// 市 /// </summary> public string City { get; set; } /// <summary> /// 市/区 代码 /// </summary> public string CityCode { get; set; } /// <summary> /// 县 /// </summary> public string Country { get; set; } /// <summary> /// 县代码 /// </summary> public string CountryCode { get; set; } } /// <summary> /// 根据出生日期,计算精确的年龄 /// </summary> /// <param name="birthDate">生日</param> /// <returns>年龄</returns> public static int CalculateAge(string birthDay, bool ageIs = true) { DateTime birthDate = DateTime.Parse(birthDay); DateTime nowDateTime = DateTime.Now; int age = nowDateTime.Year - birthDate.Year; //再考虑月、天的因素 if (ageIs) { if (nowDateTime.Month < birthDate.Month || (nowDateTime.Month == birthDate.Month && nowDateTime.Day < birthDate.Day)) { age--; } } return age; }
版权声明:本文为 魏杨杨 原创文章并发布到博客园, 除了【萬仟网】外, 其他平台欢迎转载,但必须在文章页面明显位置写明作者和出处,非常感谢。技术交流QQ群 99210270
微信扫一扫关注我公众号
一起学习,一起进步