数据持久化(保存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); } } // ==------------------------------------ }
阅读全文
0 0
- 数据持久化(保存Json文件)---Newtonsoft Json
- 数据持久化(保存Json文件)---SimpleJson
- Newtonsoft.Json解析json数据
- .net json序列化组件Json.NET(Newtonsoft.Json)
- mvc 使用Newtonsoft.Json进行序列化json数据
- mvc使用Newtonsoft.Json反序列化json数据
- C#解析Json数据(利用Newtonsoft.Json库)
- c#操作json数据使用newtonsoft.json
- Newtonsoft.Json.DLL 解析Json数据
- Newtonsoft.Json
- Newtonsoft.Json
- Newtonsoft.Json
- Newtonsoft.Json
- 数据持久化之Json
- Newtonsoft.Json 反序列化JSON数组
- 使用Json持久化保存数据,数据的写入和读出(以注册登录账号为例)_1
- Newtonsoft 获取动态key的json数据
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
- linux正则表达式 Ingeneral
- 【C#学习】静态字段、常量字段、只读字段、只读属性的相同点和不同点
- 成为java高级工程师需要什么
- 用PrintStream表示输出流。
- Unity中的image effect和Post processing
- 数据持久化(保存Json文件)---Newtonsoft Json
- webpack 3 & React 的配置 。
- 数据库表级联查询
- 车贷系统定时器模块
- 归并排序(分治)
- HEVC函数入门(20)——预测MV的获取
- Python学习异常检测
- HDU 6126 Give out candies(最小割)
- ES的性能优化的最基本的规范