MiniJosn的使用案例
来源:互联网 发布:优道网络骗局揭秘 编辑:程序博客网 时间:2024/06/07 06:33
MiniJson的使用案例
在Unity中使用的时候,需要导入一个MiniJson脚本,放在Plugins目录下:
Json文件格式如下:
[{"id":1,"name":"\u5218\u5fb7\u534e","lv":100,"hp":1000},{"id":2,"name":"\u9ece\u660e","lv":44,"hp":1000},{"id":3,"name":"\u5f20\u5b66\u53cb","lv":66,"hp":200}]
MiniJson 的代码实现 :
using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;public class JsonDemo : MonoBehaviour{ private List<Role> roleList; private List<Dictionary<string, object>> list; private List<Role> roleList_parsed; void Start() { roleList = new List<Role>(); roleList.Add(new Role { id = 1, name = "刘德华", lv = 100, hp = 1000 }); roleList.Add(new Role { id = 2, name = "黎明", lv = 44, hp = 1000 }); roleList.Add(new Role { id = 3, name = "张学友", lv = 66, hp = 200 }); ///序列化 把数据以json的形式存入一个txt文本 list = new List<Dictionary<string, object>>(); foreach (var item in roleList) { Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("id", item.id); dic.Add("name", item.name); dic.Add("lv", item.lv); dic.Add("hp", item.hp); list.Add(dic); } string jsonStr = MiniJSON.Json.Serialize(list); File.WriteAllText(Application.dataPath + "/a.txt", jsonStr); print(jsonStr); ///反序列化 print("======================"); roleList_parsed = new List<Role>(); List<object> obj = MiniJSON.Json.Deserialize(jsonStr) as List<object>; foreach (var item in obj) { print(item); Dictionary<string, object> d = item as Dictionary<string, object>; Role r = new Role(); r.id = int.Parse(d["id"].ToString()); r.name = d["name"].ToString(); roleList_parsed.Add(r); } ///把解析的结果输出下 print("==========解析的结果======="); foreach (var item in roleList_parsed) { print(string.Format("id:{0} name:{1}", item.id, item.name)); } }}public class Role{ public int id; public string name; public int age; public int lv; //等级 public int hp;}
插件下载链接:https://pan.baidu.com/s/1i4V1v1b 密码:f82b
阅读全文
0 0
- MiniJosn的使用案例
- Flume 的使用案例
- FileUpload的使用案例
- Comparator的使用案例
- GPUImage的使用案例
- jedis的使用案例
- Bmob的使用案例
- Spinner的使用案例
- echarts2的使用案例
- getsockopt的使用案例
- strstr的使用案例
- SoundPool的使用案例
- WebView 的使用案例
- DirectoryInfo的使用案例
- git的使用案例
- echarts2的使用案例
- QSS的使用案例
- PHPExcel的使用案例:
- MFC基于单文档写unicode的文件
- 通过live555实现H264 RTSP直播(Windows版)
- ED灯具CCC认证中涉及安全要求的常见问题
- 猜数字小游戏
- 拦截器
- MiniJosn的使用案例
- 递归思想求解稀疏多项式的值
- TabLayout加载多个Fragment
- 浏览器做手机页面的调试
- 数据结构实验之查找五:平方之哈希表
- hadoop分布式搭建
- 欧几里得算法
- 澳规插脚测试AS/NZS 3112
- 1.5不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符,怎么办?