(Newtonsoft)Json增删改查(这是单个对象)

 public static class JsonHelper
    {
        #region 字段
        private static string json;
        public static string path;
        #endregion

        #region 构造函数
        static JsonHelper()
        {
            if (string.IsNullOrEmpty(path))
            {
                string currentDirectory = Directory.GetCurrentDirectory();
                path = currentDirectory + "\\LocalData.Data";
            }
            if (!File.Exists(path))
            {
                FileStream fs = File.Create(path);
                fs.Close();
            }
            json = LoadFromFile(path);
        }
        #endregion  

        #region 获取
        public static object GetData(string propertyName)
        {
            return GetDate(propertyName);
        }
        #endregion

        #region 保存
        public static void SetData(string propertyName, object propertyValue)
        {
            SetData(ref json, propertyName, propertyValue);
            SaveToFile(path);
        }
        #endregion

        #region 移除
        /// <summary>
        /// 根据key值移除键值对
        /// </summary>
        /// <param name="propertyName">key值</param>
        public static void RemovePropertyName(string propertyName)
        {
            Remove(ref json, propertyName);
            SaveToFile(path);
        }
        public static void ClearData()
        {
            json = string.Empty;
            SaveToFile(path);
        }
        #endregion

        #region 内部方法
        private static void Add(ref string json, string propertyName, object propertyValue)
        {
            JObject jsonObj = JObject.Parse(json);
            jsonObj.Add(propertyName, JToken.FromObject(propertyValue));
            json = jsonObj.ToString();
        }

        private static void Update(ref string json, string propertyName, object propertyValue)
        {
            JObject jsonObj = JObject.Parse(json);
            jsonObj[propertyName] = JToken.FromObject(propertyValue);
            json = jsonObj.ToString();
        }

        private static object GetValue(string json, string propertyName)
        {
            dynamic jsonObj = JsonConvert.DeserializeObject(json);
            return jsonObj[propertyName];
        }

        private static void Remove(ref string json, string propertyName)
        {
            dynamic jsonObj = JsonConvert.DeserializeObject(json);
            jsonObj.Remove(propertyName);
            json = JsonConvert.SerializeObject(jsonObj);
        }

        private static void SetData(ref string json, string propertyName, object propertyValue)
        {
            JObject jsonObj;
            if (!string.IsNullOrEmpty(json))
            {
                jsonObj = JObject.Parse(json);
                if (jsonObj[propertyName] != null)
                {
                    jsonObj[propertyName] = JToken.FromObject(propertyValue);
                }
                else
                {
                    jsonObj.Add(propertyName, JToken.FromObject(propertyValue));
                }
            }
            else
            {
                jsonObj = new JObject
                {
                    { propertyName, JToken.FromObject(propertyValue) }
                };

            }
            json = jsonObj.ToString();
        }

        private static object GetDate(string propertyName)
        {
            if (!string.IsNullOrEmpty(json))
            {
                dynamic jsonObj = JsonConvert.DeserializeObject(json);
                return jsonObj[propertyName];
            }
            return null;
        }

        private static void SaveToFile(string filePath)
        {
            File.WriteAllText(filePath, json);
        }

        private static string LoadFromFile(string filePath)
        {
            return File.ReadAllText(filePath);
        }
        #endregion


    }

 

posted @ 2023-01-28 13:41  园友2288976  阅读(261)  评论(0编辑  收藏  举报