Unity JSON 序列化
来源:互联网 发布:程序员注释 编辑:程序博客网 时间:2024/06/05 22:45
测试 类
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.csdn.net/detail/microsoftmsdnnet/9626477
0 0
- Unity JSON 序列化
- Unity中JSON序列化与反序列化
- Unity Json的序列化和反序列化
- Unity实现json格式的序列化并发布
- Unity序列化之XML,JSON--------合成与解析
- Unity序列化之XML,JSON--------合成与解析
- 【Unity&JSON】.net JSON 数组 序列化和反序列化,并 创建玩家数据列表
- [Unity&继承&序列化&JSON]在JSON数据中,子类继承父类问题
- Unity使用JsonFx进行JSON (反)序列
- Unity中的序列化
- unity [serializefiled]序列化
- Unity序列化
- Unity中Json 解析(序列化和反序列化)
- Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器
- 序列化和json
- Json序列化
- JSON序列化类
- 序列化与json
- 打包Jar包,混淆Jar包,合并多个Jar包
- 十二上机5
- Spring学习总结(18)——Spring整合Mysql数据库一主多从、多主多从配置
- opengles之3D模型加载(obj模型文件)
- 关于Recycleview的Inconsistency detected. Invalid view holder adapter positionViewHolder
- Unity JSON 序列化
- cocos2dx physics物理引擎碰撞详解
- 对称加密----AES和DES加密、解密
- HDU1561 这应该也是一个很裸的树上背包...
- 解决Listview的Item与按钮或文本框焦点的问题
- Learning_neutron_command
- ReactiveCocoa入门教程——第二部分
- com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource
- POJ 1821 - Fence