数据持久化(保存Json文件)---SimpleJson
来源:互联网 发布:h3c防火墙web配置 端口 编辑:程序博客网 时间:2024/03/28 17:49
using System.Collections;using System.Collections.Generic;using UnityEngine;using SimpleJSON;using System.IO;using System;using GFW;using System.Text;public class GSetting { // json 文件 static string _savePath = Application.dataPath + "/../WriteablePath/json/simple_json.json"; public static void SetValue(string dataKey, object dataValue) { if (string.IsNullOrEmpty(dataKey)) { Debug.Log(" is null !"); return; } if (Refresh(dataKey, dataValue)) { return; } JSONObject _newData = new JSONObject(); _newData.Add(dataKey, Serialize(dataValue)); if (_newData != null) { string sign = _newData.ToString() + "\r\n"; GFileUtil.CreateDir(GFileUtil.GetDirPath(_savePath)); File.AppendAllText(_savePath, sign, Encoding.UTF8); Debug.Log(" Save Success !"); } } public static object GetValue(string dataKey, object dataValue = null) { if (GFileUtil.IsFileExists(_savePath)) { try { StreamReader _readData = new StreamReader(_savePath); string _tempData = _readData.ReadLine(); while (_tempData != null) { var obj = JSONNode.Parse(_tempData); JSONObject _jsonData = (JSONObject)obj; JSONNode _tempValue = _jsonData[dataKey]; if (_tempValue != null) { //dataValue = _tempValue; dataValue = Judge(_tempValue); break; } _tempData = _readData.ReadLine(); } _readData.Close(); } catch (Exception e) { Debug.Log(e.ToString()); } } return dataValue; } static object Judge(JSONNode jsonObj) { object obj = null; if (jsonObj != null) { if (jsonObj.Tag == JSONNodeType.Boolean) { obj = jsonObj.AsBool; } else if (jsonObj.Tag == JSONNodeType.Float) { obj = jsonObj.AsFloat; } else if (jsonObj.Tag == JSONNodeType.Int) { obj = jsonObj.AsInt; } else if (jsonObj.Tag == JSONNodeType.String) { obj = jsonObj.Value; } } return obj; } public static void SetUserData(string userId, string dataKey,object dataValue) { Type tt = dataValue.GetType(); if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(dataKey)) { Debug.Log(" null "); return; } if (Refresh(dataKey, dataValue,userId)) { return; } JSONObject _parentData = new JSONObject(); JSONObject _sonData = new JSONObject(); _sonData.Add(dataKey, Serialize(dataValue)); _parentData.Add(userId, _sonData); if (_parentData != null) { string sign = _parentData.ToString() + "\r\n"; GFileUtil.CreateDir(GFileUtil.GetDirPath(_savePath)); File.AppendAllText(_savePath, sign, Encoding.UTF8); Debug.Log(" Save Success !"); } } public static object GetUserData(string userID, string dataKey, object dataValue = null) { if (GFileUtil.IsFileExists(_savePath)) { try { StreamReader _readData = new StreamReader(_savePath); string _tempData = _readData.ReadLine(); while (_tempData != null) { JSONNode _jsonData = JSONNode.Parse(_tempData); JSONNode _tempValue = _jsonData[userID]; if (_tempValue!=null) { JSONNode tempStr = _tempValue[dataKey]; if (tempStr != null) { //dataValue = tempStr; dataValue = Judge(tempStr); break; } } _tempData = _readData.ReadLine(); } _readData.Close(); } catch (Exception e) { Debug.Log(e.ToString()); } } return dataValue; } public static JSONNode Serialize(object dataValue) { JSONNode temp = null; if (dataValue != null) { Type valueType = dataValue.GetType(); if (valueType == typeof(string)) { temp = new JSONString(dataValue as string); } else if (valueType == typeof(bool)) { temp = new JSONBool((bool)dataValue); } else if (valueType == typeof(float)) { temp = new JSONFloat((float)dataValue); } else if (valueType == typeof(int)) { temp = new JSONInt((int)dataValue); } else { Debug.LogError("unsupport type " + valueType.Name); } } return temp; } public static bool Refresh(string dataKey, object dataValue, string userID = null) { List<JSONObject> userConfigData = new List<JSONObject>(); if (GFileUtil.IsFileExists(_savePath)) { try { StreamReader _readData = new StreamReader(_savePath); string _tempData = _readData.ReadLine(); while (_tempData != null) { var obj = JSONNode.Parse(_tempData); JSONObject _jsonData = (JSONObject)obj; userConfigData.Add(_jsonData); _tempData = _readData.ReadLine(); } _readData.Close(); } catch (Exception e) { Debug.Log(e.ToString()); } bool isRepet = false; string _newContent = ""; foreach (JSONObject item in userConfigData) { string tempStr = ""; tempStr = item.ToString(); if (userID != null) { JSONNode _tempValue = item[userID]; if (_tempValue != null) { JSONNode _temp = _tempValue[dataKey]; if (_temp != null) { JSONObject _parentData = new JSONObject(); JSONObject _sonData = new JSONObject(); _sonData.Add(dataKey, Serialize(dataValue)); _parentData.Add(userID, _sonData); tempStr = _parentData.ToString(); isRepet = true; } } } else { JSONNode _tempValue = item[dataKey]; if (_tempValue != null) { JSONObject _refData = new JSONObject(); _refData.Add(dataKey, Serialize(dataValue)); tempStr = _refData.ToString(); isRepet = true; } } _newContent += tempStr + "\r\n"; } if (!string.IsNullOrEmpty(_newContent) && isRepet) { GFileUtil.CreateDir(GFileUtil.GetDirPath(_savePath)); File.WriteAllText(_savePath, _newContent, Encoding.UTF8); Debug.Log(" Refresh Success !"); return true; } } return false; }}要导入 SimpleJson 插件
阅读全文
0 0
- 数据持久化(保存Json文件)---SimpleJson
- 数据持久化(保存Json文件)---Newtonsoft Json
- 数据持久化保存(文件读写,归档,sql读写)
- 数据持久化方案(四)文件保存
- 数据持久化保存
- Java 小例子:数据持久化(保存数据到文件)
- simplejson json
- 数据持久化基础知识——文件保存方案
- 使用Json持久化保存数据,数据的写入和读出(以注册登录账号为例)_1
- 数据的持久化保存
- iOS(Swift)使用字典NSMutableDictionary保存数据到文件(持久化)
- iOS 使用字典NSMutableDictionary保存数据到文件(持久化)~ Swift
- Python持久存储--数据保存到文件
- 数据持久化之Json
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
- Android02_02喜爱的数据持久化,保存数据到rom文件
- 1 android下的数据持久化,保存数据到rom文件
- Python中Json文件的读入和写入以及simplejson
- css 设置height:100%问题
- uva 1267
- 一队人根据令牌分组,求最强队的实力
- 内存管理 一
- Android开发培训(07)--相机多媒体
- 数据持久化(保存Json文件)---SimpleJson
- 响应式布局的实现
- Angular 4.0_在线竞拍网站(2)_创建项目
- TCP/IP详解
- C#205课的主要内容
- Hinton Neural Networks课程笔记3b:线性神经元的误差曲面
- codevs 1219 骑士游历 DP 解题报告
- 优秀惬意的文章会让你的身心愉悦!
- JAVA的进阶学习之路,从入门到中级