Unity 保存Json数据到本地文件

来源:互联网 发布:淘宝的欧莱雅是真的吗 编辑:程序博客网 时间:2024/05/22 13:10

一、先导入Json 解析库;

下载地址:http://download.csdn.net/detail/u014076894/9606309


二、开始代码的编写;

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //命名空间  
  2. using System.IO;  
  3. using System.Collections.Generic;  
  4. using LitJson;  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //相关变量声明:  
  2.     private static string mFolderName;  
  3.     private static string mFileName;  
  4.     private static Dictionary<stringstring> Dic_Value = new Dictionary<stringstring>();  
  5.   
  6.     private static string FileName {  
  7.         get {  
  8.             return Path.Combine(FolderName, mFileName);  
  9.         }  
  10.     }  
  11.   
  12.     private static string FolderName {  
  13.         get {  
  14.             return Path.Combine(Application.persistentDataPath, mFolderName);  
  15.         }  
  16.     }  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //初始化方法 如有需要,可重载初始化方法  
  2.     public static void Init(string pFolderName, string pFileName) {  
  3.         mFolderName = pFolderName;  
  4.         mFileName = pFileName;  
  5.         Dic_Value.Clear();  
  6.         Read();  
  7.     }  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //读取文件及json数据加载到Dictionary中  
  2.     private static void Read() {  
  3.         if(!Directory.Exists(FolderName)) {  
  4.             Directory.CreateDirectory(FolderName);  
  5.         }  
  6.         if(File.Exists(FileName)) {  
  7.             FileStream fs = new FileStream(FileName, FileMode.Open);  
  8.             StreamReader sr = new StreamReader(fs);  
  9.             JsonData values = JsonMapper.ToObject(sr.ReadToEnd());  
  10.             foreach(var key in values.Keys) {  
  11.                 Dic_Value.Add(key, values[key].ToString());  
  12.             }  
  13.             if(fs != null) {  
  14.                 fs.Close();  
  15.             }  
  16.             if(sr != null) {  
  17.                 sr.Close();  
  18.             }  
  19.         }  
  20.     }  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //将Dictionary数据转成json保存到本地文件  
  2.     private static void Save() {  
  3.         string values = JsonMapper.ToJson(Dic_Value);  
  4.         Debug.Log(values);  
  5.         if(!Directory.Exists(FolderName)) {  
  6.             Directory.CreateDirectory(FolderName);  
  7.         }  
  8.         FileStream file = new FileStream(FileName, FileMode.Create);  
  9.         byte[] bts = System.Text.Encoding.UTF8.GetBytes(values);  
  10.         file.Write(bts,0,bts.Length);  
  11.         if(file != null) {  
  12.             file.Close();  
  13.         }  
  14.     }  

到此,简单的保存方法基本完成了。

三、举例使用;

拿读写字符串为例:

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //判断当前是否存在该key值  
  2.     public static bool HasKey(string pKey) {  
  3.         return Dic_Value.ContainsKey(pKey);  
  4.     }  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //读取string值  
  2.     public static string GetString(string pKey) {  
  3.         if(HasKey(pKey)) {  
  4.             return Dic_Value[pKey];  
  5.         } else {  
  6.             return string.Empty;  
  7.         }  
  8.     }  

[csharp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //保存string值  
  2.     public static void SetString(string pKey, string pValue) {  
  3.         if(HasKey(pKey)) {  
  4.             Dic_Value[pKey] = pValue;  
  5.         } else {  
  6.             Dic_Value.Add(pKey, pValue);  
  7.         }  
  8.         Save();  
  9.     }  

0 0
原创粉丝点击