Unity JSON 序列化
发表于2018-04-22
Unity中的Json的序列化,支持嵌套使用,可以把json字符串转成对象,把对象转成json字符串。
测试 类
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; } }
来自:https://blog.csdn.net/microsoftmsdnnet/article/details/52489450
如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引