赞
踩
单行注释 //
多行注释 /* 内容 */
- //注释内容
- /*
- 多行注释
- 123
- e1
- ds
- */
- /*
大括号 {} 对象
中括号 [] 数组
冒号 : 键值对对应关系
逗号 , 数据分割
双引号 "" 键名/字符串
“
"键名":值内容
数字(整数或浮点)
字符串
true或false
数组
对象
null
- {
- "name":"小周",
- "age":24,
- "sex":true,
- "ids":[1, 2, 3],
- "students":[
- {"name":"小红", "age":18, "sex":false},
- {"name":"小明", "age":18, "sex":true}
- ],
- "home":{
- "address":"重庆市垫江县",
- "street":""
- },
- "son":{
- "name":"小周周",
- "age":7,
- "sex":true
- }
- }

JsonUtility是Unity自带的用于解析Json的公共类
它可以将内存中对象序列化为Json格式的字符串,也可以将Json字符串反序列化为类对象
1) 存储字符串到指定路径文件中
File.WriteAllText(arg1, arg2)
第一个参数 填写的是存储的路径
第二个参数 填写的是 存储的字符串内容
2) 在指定路径文件中读取字符串
File.ReadAllText(path)
- //1.存储字符串到指定路径文件中
- //第一个参数 填写的是存储的路径!
- //第二个参数 填写的是 存储的字符串内容
- string path = Application.persistentDataPath + "/Test.json";
- File.WriteAllText(path, "大哥存储的JSON文件");
- Debug.Log(path);
-
- //2.在指定路径文件中读取字符串
- string str = File.ReadAllText(path);
- Debug.Log(str);
序列化:把内存中的数据 存储到硬盘上
JsonUtility.ToJson(对象)
- [Serializable]
- public class Student
- {
- public int age;
- public string name;
-
- public Student(int age, string name)
- {
- this.age = age;
- this.name = name;
- }
- }
-
- public class MrTang
- {
- public string name;
- public int age;
- public bool sex;
- public float testFloat;
- public double testDouble;
-
- public int[] ids;
- public List<int> ids2;
- public Dictionary<int, string> dic;
- public Dictionary<string, string> dic2;
-
- public Student s1;
- public List<Student> s2s;
-
- }
-
- [Serializable]
- public class RoleInfo
- {
- public int hp;
- public int speed;
- public int volume;
- public string resName;
- public int scale;
- }
-
- //序列化:把内存中的数据 存储到硬盘上
- //方法:
- //JsonUtility.ToJson(对象)
- MrTang t = new MrTang();
- t.name = "小周";
- t.age = 18;
- t.sex = true;
- t.testFloat = 1.1f;
- t.testDouble = 1.5;
-
- t.ids = new int[] { 1, 2, 3 };
- t.ids2 = new List<int>() { 2, 3, 5, 6 };
- t.dic = new Dictionary<int, string>() {
- {1, "123" },
- {2, "234" }
- };
- t.dic2 = new Dictionary<string, string>() {
- {"12", "123" },
- {"sds", "234" }
- };
-
- t.s1 = new Student(18, "adada");
- t.s2s = new List<Student>() { new Student(12, "小红"), new Student(17, "小明") };
-
- //]sonutility提供了线程的方法 可以把类对象 序列化为 json字符串
- string jsonStr = JsonUtility.ToJson(t);
- File.WriteAllText(Application.persistentDataPath + "/MrTang.json", jsonStr);

注意:
反序列化:把硬盘上的数据 读取到内存中
JsonUtility.FremJson(字符串)
- //读取文件中的 Json字符串
- jsonStr = File.ReadAllText(Application.persistentDataPath + "/MrTang.json");
- //使用Json字符串内容 转换成类对象
- MrTang t2 = JsonUtility.FromJson(jsonStr, typeof(MrTang)) as MrTang;
- MrTang t3 = JsonUtility.FromJson<MrTang>(jsonStr);
注意:
JsonMapper.ToJson(对象)
- //JsonMapper.ToJson(对象)
- MrTang2 t = new MrTang2();
- t.name = "下周";
- t.age = 18;
- t.sex = true;
- t.testFloat = 1.55f;
- t.testDouble = 1.4;
-
- t.ids = new int[] { 1, 2, 3 };
- t.ids2 = new List<int>() { 2, 3, 5, 6 };
- //t.dic = new Dictionary<int, string>() {
- // {1, "123" },
- // {2, "234" }
- //};
- t.dic2 = new Dictionary<string, string>() {
- {"12", "123" },
- {"sds", "234" }
- };
-
- //t.s1 = new Student2(18, "adada");
- t.s1 = null;
- t.s2s = new List<Student2>() { new Student2(12, "小红"), new Student2(17, "小明") };
-
- string jsonStr = JsonMapper.ToJson(t);
- Debug.Log(Application.persistentDataPath);
- File.WriteAllText(Application.persistentDataPath + "/MrTang2.json", jsonStr);

注意:
JsonMapper.ToObject(字符串)
- jsonStr = File.ReadAllText(Application.persistentDataPath + "/MrTang2.json");
- JsonData data = JsonMapper.ToObject(jsonStr);
- Debug.Log(data["name"]);
- Debug.Log(data["age"]);
-
- MrTang2 st2 = JsonMapper.ToObject<MrTang2>(jsonStr);
注意:
另外需要LitJson代码
- using LitJson;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
-
- /// <summary>
- /// 序列化和反序列化JSON时 使用哪一种方案
- /// </summary>
- public enum JsonType
- {
- JsonUtility,
- LitJson
- }
-
- /// <summary>
- /// Json数据管理类 主要用于进行 json的序列化存储到硬盘 和 反序列化从硬盘中读取到内存中
- /// </summary>
- public class JsonMgr
- {
- private static JsonMgr instance = new JsonMgr();
- public static JsonMgr Instance
- {
- get
- {
- return instance;
- }
- }
- private JsonMgr() { }
-
-
- /// <summary>
- /// 存储JSON数据 序列化
- /// </summary>
- public void SavaData(object data, string fileName, JsonType jsonType = JsonType.LitJson)
- {
- //确定存储路径
- string path = Application.persistentDataPath + "/" + fileName + ".json";
- Debug.Log(path);
-
- //序列化 得到Json字符串
- string jsonStr = "";
- switch (jsonType)
- {
- case JsonType.JsonUtility:
- jsonStr = JsonUtility.ToJson(data);
- break;
- case JsonType.LitJson:
- jsonStr = JsonMapper.ToJson(data);
- break;
- default:
- break;
- }
-
- //把序列化的Json字符串 存储到指定路径中
- File.WriteAllText(path, jsonStr);
- }
-
- /// <summary>
- /// 读取指定文件中的 json数据 反序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="fileName"></param>
- /// <param name="jsonType"></param>
- /// <returns></returns>
- public T LoadData<T>(string fileName, JsonType jsonType = JsonType.LitJson) where T : class, new()
- {
- //确定从哪个路径读取
- //首先先判断 默认数据文件夹中是否有我们想要的数据 如果有 就从中获取
- string path = Application.persistentDataPath + "/" + fileName + ".json";
- //先判断 是否存在这个文件
- //如果不存在默认文件 就从 读写文件夹中去寻找
- if (!File.Exists(path))
- path = Application.streamingAssetsPath + "/" + fileName + ".json";
- //如果读写文件夹中都还没有 那就返回一个默认对象
- if (!File.Exists(path))
- return new T();
-
- //进行反序列化
- string jsonStr = File.ReadAllText(path);
-
- T data = default(T);
-
- switch (jsonType)
- {
- case JsonType.JsonUtility:
- data = JsonUtility.FromJson<T>(jsonStr);
- break;
- case JsonType.LitJson:
- data = JsonMapper.ToObject<T>(jsonStr);
- break;
- default:
- break;
- }
-
- //把对象返回出去
- return data;
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。