コード例 #1
0
 /// <summary>
 /// 读取更新时间
 ///     从 JSON 文件
 /// </summary>
 /// <param name="icp">ICacheParam</param>
 /// <returns></returns>
 public static DateTime ReadWriteTimeFormJson(ICacheParam icp)
 {
     try
     {
         //JSON路径
         string   jsonPath = icp.GetJsonFile();
         FileInfo fi       = new FileInfo(jsonPath);
         return(fi.LastWriteTime);
     }
     catch (System.Exception ex)
     {
         throw new ParamException("读取参数文件错误", ex);
     }
 }
コード例 #2
0
        /// <summary>
        /// 读取参数列表
        ///     从 JSON 文件
        /// </summary>
        /// <param name="icp">ICacheParam</param>
        /// <param name="listProperty">(可选)属性列表</param>
        /// <returns></returns>
        public static List <AqiParam> CreateListFormJson(ICacheParam icp, params string[] listProperty)
        {
            List <AqiParam> listParam    = new List <AqiParam>();
            string          propertyPath = String.Join(".", listProperty);

            try
            {
                //JSON路径
                string jsonPath = icp.GetJsonFile();
                if (!File.Exists(jsonPath))
                {
                    return(listParam);
                }

                //读取JSON
                StreamReader sr       = new StreamReader(jsonPath);
                string       jsonText = sr.ReadToEnd();
                //转JSON Object
                JObject jo = JObject.Parse(jsonText);
                JToken  jt = jo.SelectToken(propertyPath);

                if (jt == null || !jt.HasValues)
                {
                    return(null);
                }
                else if (jt is JArray)
                {
                    //读取集合(任意个参数)
                    JEnumerable <JToken> je     = jt.Children();
                    AqiParam             baseAP = null;
                    foreach (JToken j in je)
                    {
                        AqiParam ap = createParamFormJsonObject(j as JObject, baseAP);
                        if (ap != null)
                        {
                            if (ap.IsTemplate)
                            {
                                baseAP = ap;
                            }
                            else
                            {
                                listParam.Add(ap);
                            }
                        }
                    }
                }
                else if (jt is JObject)
                {
                    //读取对象(仅一个参数)
                    AqiParam ap = createParamFormJsonObject(jt as JObject);
                    if (ap != null)
                    {
                        listParam.Add(ap);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw new ParamException("参数创建错误", ex);
            }

            return(listParam);
        }