测试 类
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class App : MonoBehaviour
{
PlayInfo info;
public void Awake()
{
ToJson();
FromJson();
}
// 序列化
void ToJson()
{
info = new PlayInfo("Unity", "123456", 234, "Json");
string infoJson = JsonTool.ToJson(info);
string path = FileHelp.PersistentWrite(info.AccountName + ".txt", infoJson);
Debug.Log(path);
}
//反序列化
void FromJson()
{
info = new PlayInfo();
info.DeSerialize(JsonTool.DictionaryFromJson(FileHelp.PersistentLoadOrReard("Unity.txt")));
Debug.Log(info.AccountName);
}
}
序列化对象
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class PlayInfo {
public string AccountName;
public string PassWord;
public int Gold;
public string NickName;
public string Grade;
public int Score;
public PlayInfo()
{
}
public PlayInfo(string accountName,string password,int gold,string nickName,string grade = "初级",int score = 0)
{
this.AccountName = accountName;
this.PassWord = password;
this.Gold = gold;
this.NickName = nickName;
this.Grade = grade;
this.Score = score;
}
//反序列化
public void DeSerialize(Dictionary<string ,object> obj)
{
foreach (string item in obj.Keys)
{
switch (item)
{
case "AccountName":
AccountName = JsonTool.GetStringValue(obj,item);
break;
case "PassWord":
PassWord = JsonTool.GetStringValue(obj, item);
break;
case "Gold":
Gold = JsonTool.GetIntValue(obj, item);
break;
case "NickName":
NickName = JsonTool.GetStringValue(obj, item);
break;
case "Grade":
Grade = JsonTool.GetStringValue(obj, item);
break;
case "Score":
Score = JsonTool.GetIntValue(obj, item);
break;
default:
break;
}
}
}
}
JSON 工具
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class JsonTool {
public static string ToJson(object obj)
{
return Json.Serialize(obj);
}
public static object FromJson(string str)
{
return Json.Deserialize(str);
}
public static Dictionary<string, object> DictionaryFromJson(string json)
{
return (Dictionary<string, object>)Json.Deserialize(json);
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="dic">字典</param>
/// <param name="key">字典里具体键</param>
/// <returns></returns>
public static int GetIntValue(Dictionary<string, object> dic, string key)
{
object value = GetValue(dic, key);
if (value == null)
{
return 0;
}
int intValue;
if (int.TryParse(value.ToString(), out intValue) == false)
{
Debug.Log(int.Parse(value.ToString()));
Debug.LogError("Value 转换失败 " + intValue);
}
return intValue;
}
public static string GetStringValue(Dictionary<string, object> dic, string key)
{
object value = GetValue(dic, key);
if (value == null)
{
return null;
}
return value.ToString();
}
public static object GetValue(Dictionary<string, object> dic, string key)
{
if (dic == null)
{
Debug.LogError("Dictionary == null");
return null;
}
object value;
if (dic.TryGetValue(key, out value))
{
return value;
}
Debug.LogError("Key not reference");
return null;
}
}
还有一个 JSON.cs 类过大,存放在工程里。
工程地址:http://download.youkuaiyun.com/detail/microsoftmsdnnet/9626477