数据持久化(保存Json文件)---Newtonsoft Json

来源:互联网 发布:淘宝店铺宝贝怎么打折 编辑:程序博客网 时间:2024/04/27 22:28

Newtonsoft Json 库

using System.Collections;using System.Collections.Generic;using UnityEngine;using System;using System.IO;using System.Text;using GFW;using Newtonsoft.Json;//public class UserConfigData//{//    public Dictionary<string, object> configDataDic = new Dictionary<string, object>();//}public class UserPersonData{    public string curUserID;    public Dictionary<string, object> curUserDataDic = new Dictionary<string, object>();}public class GSetting {    static string curUserID = "data";    static string _saveDataPath = Application.dataPath + "/../WriteablePath/json/_current_userdata.json";    // ----SetConfig    public static void SetValue(string dataKey, object dataValue)    {        if (string.IsNullOrEmpty(dataKey))        {            Debug.Log(" is null !");            return;        }        if (RefreshData(dataKey,dataValue))        {            return;        }        UserPersonData _newUserConfigData = new UserPersonData();        _newUserConfigData.curUserDataDic[dataKey] = dataValue;        string _saveData = JsonConvert.SerializeObject(_newUserConfigData);        if (!string.IsNullOrEmpty(_saveData))        {            _saveData = _saveData + "\r\n";            GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));            File.AppendAllText(_saveDataPath, _saveData, Encoding.UTF8);            Debug.Log(" Save Success !");        }    }    public static object GetValue(string dataKey, object dataValue = null)    {        if (GFileUtil.IsFileExists(_saveDataPath))        {            try            {                StreamReader _readData = new StreamReader(_saveDataPath);                string _tempData = _readData.ReadLine();                while (_tempData != null)                {                    UserPersonData _jsonData = JsonConvert.DeserializeObject<UserPersonData>(_tempData);                    if (_jsonData.curUserDataDic.ContainsKey(dataKey))                    {                        dataValue = _jsonData.curUserDataDic[dataKey];                        break;                    }                    _tempData = _readData.ReadLine();                }                _readData.Close();            }            catch (Exception e)            {                Debug.Log(e.ToString());            }        }        return dataValue;    }    // ----SetUserData    public static void SetUserData(string dataKey, object dataValue)    {        if (string.IsNullOrEmpty(dataKey))        {            Debug.Log(" null ");            return;        }        if (RefreshData(dataKey, dataValue))        {            return;        }        UserPersonData _newUserPersonData = new UserPersonData();        _newUserPersonData.curUserID = curUserID;        _newUserPersonData.curUserDataDic[dataKey] = dataValue;        string _saveData = JsonConvert.SerializeObject(_newUserPersonData);        if (!string.IsNullOrEmpty(_saveData))        {            _saveData = _saveData + "\r\n";            GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));            File.AppendAllText(_saveDataPath, _saveData, Encoding.UTF8);            Debug.Log(" Save Success !");        }    }    public static object GetUserData(string dataKey, object dataValue = null)    {        if (GFileUtil.IsFileExists(_saveDataPath))        {            try            {                StreamReader _readData = new StreamReader(_saveDataPath);                string _tempData = _readData.ReadLine();                while (_tempData != null)                {                    UserPersonData _jsonData = JsonConvert.DeserializeObject<UserPersonData>(_tempData);                    if (_jsonData.curUserID == curUserID && _jsonData.curUserDataDic.ContainsKey(dataKey))                    {                        dataValue = _jsonData.curUserDataDic[dataKey];                        break;                    }                    _tempData = _readData.ReadLine();                }                _readData.Close();            }            catch (Exception e)            {                Debug.Log(e.ToString());            }        }        return dataValue;    }    // ----Refresh    public static bool RefreshData(string dataKey, object dataValue)    {        List<UserPersonData> _userPersonDataList = new List<UserPersonData>();        if (GFileUtil.IsFileExists(_saveDataPath))        {            try            {                StreamReader _readData = new StreamReader(_saveDataPath);                string _tempData = _readData.ReadLine();                while (_tempData != null)                {                    UserPersonData _jsonData = JsonConvert.DeserializeObject<UserPersonData>(_tempData);                    _userPersonDataList.Add(_jsonData);                    _tempData = _readData.ReadLine();                }                _readData.Close();            }            catch (Exception e)            {                Debug.Log(e.ToString());            }            string refContent = "";            bool isRepet = false;            foreach (UserPersonData item in _userPersonDataList)            {                string _tempTxt = "";                _tempTxt = JsonConvert.SerializeObject(item);                if (item.curUserID == null && item.curUserDataDic.ContainsKey(dataKey))                {                    UserPersonData _newData = new UserPersonData();                    _newData.curUserDataDic[dataKey] = dataValue;                    _tempTxt = JsonConvert.SerializeObject(_newData);                    isRepet = true;                }                else if (item.curUserID == curUserID && item.curUserDataDic.ContainsKey(dataKey))                {                    UserPersonData _newData = new UserPersonData();                    _newData.curUserID = curUserID;                    _newData.curUserDataDic[dataKey] = dataValue;                    _tempTxt = JsonConvert.SerializeObject(_newData);                    isRepet = true;                }                refContent += _tempTxt + "\r\n";            }            if (!string.IsNullOrEmpty(refContent) && isRepet)            {                GFileUtil.CreateDir(GFileUtil.GetDirPath(_saveDataPath));                File.WriteAllText(_saveDataPath, refContent, Encoding.UTF8);                Debug.Log(" Refresh Success !");                return true;            }        }        return false;    }    // ----DeleteAll    public static void DeleteAll()    {        if (GFileUtil.IsFileExists(_saveDataPath))        {            string deleteData = "";            File.WriteAllText(_saveDataPath, deleteData, Encoding.UTF8);        }    }    // ==------------------------------------    }