首页 > 编程语言 >C#Newtonsoft (本地储存),增删改查 帮助类 比较简单(这是嵌套对象)

C#Newtonsoft (本地储存),增删改查 帮助类 比较简单(这是嵌套对象)

时间:2023-02-16 17:36:47浏览次数:37  
标签:jsonObj Newtonsoft string C# propertyName 改查 json static id

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

            #region 构造函数
            static LocalSetupHelper()
            {
                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(Enum id,string propertyName)
            {
                return GetDatas(id, propertyName);
            }
            #endregion

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

            #region 移除
            /// <summary>
            /// 根据key值移除键值对
            /// </summary>
            /// <param name="propertyName">key值</param>
            public static void RemovePropertyName(Enum id,string propertyName)
            {
                Remove(ref json,id, 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, Enum id,string propertyName=null)
            {
                dynamic jsonObj = JsonConvert.DeserializeObject(json);
                if (propertyName==null)
                {
                    jsonObj.Remove(propertyName);
                }
                else
                {
                    //jsonObj["总台"].Value<JObject>().Remove("账号");
                    jsonObj[id.ToString()].Remove(propertyName);//13.0版本
                }
                json = JsonConvert.SerializeObject(jsonObj);
            }

            private static void SetData(ref string json,Enum id,string propertyName, object propertyValue)
            {
                JObject fatherObj = new JObject();

                JObject jsonObj;
                if (!string.IsNullOrEmpty(json))
                {
                    jsonObj = JObject.Parse(json);

                    try
                    {
                        if (jsonObj.Count != 0 || jsonObj[id.ToString()][propertyName] != null)
                        {
                            jsonObj[id.ToString()][propertyName] = JToken.FromObject(propertyValue);
                            fatherObj = jsonObj;
                           
                        }
                        else
                        {

                            jsonObj.Add(propertyName, JToken.FromObject(propertyValue));
                            fatherObj.Add(id.ToString(), jsonObj);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteError(typeof(LocalSetupHelper), ex);
                    }
                    
                }
                else
                {
                    jsonObj = new JObject
                    
                {
                    { propertyName, JToken.FromObject(propertyValue) }
                    
                };
                  fatherObj.Add(id.ToString(), jsonObj);

                }
                json = fatherObj.ToString();
            }

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

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

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


        
    }

 

标签:jsonObj,Newtonsoft,string,C#,propertyName,改查,json,static,id
From: https://www.cnblogs.com/jiaozai891/p/17127521.html

相关文章