Json配置规则
用什么编辑Json
只要能打开文档的文件都能编辑Json
常用的Json编辑方式有:
- 记事本
- Sublime Text
- 网页上的Json编辑器
注释
只有在json5规范中才允许使用注释
语法规则
json格式是一种键值对结构
Json与C#数据的对应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| using System.Collections.Generic; using UnityEngine;
namespace Json { public class Person : MonoBehaviour { public string name; public int age; public bool sex; public Home home; public List<int> ids; public List<Person> friends; } public class Home { public string address; public string street; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| { "name":"Pditine", "age":20, "sex":true, "home": { "address":"beijing", "street":"jianguolu" }, "ids":[1,2,3,4,5], "friends":[ { "name":"friend1", "age":19, "sex":true, "home": { "address":"beijing", "street":"jianguolu" }, "ids":[1,2,3,4,6], "friends":null } ,{ "name":"friend2", "age":21, "sex":false, "home": { "address":"shanghai", "street":"jianguolu" }, "ids":[1,2,3,4,7], "friends":null } ] }
|
Excel转Json
在Excel中配置数据
现阶段就不要研究反射或者动unity编辑器了,你可以用已有的工具完成转换
hp |
speed |
name |
4 |
5 |
play1 |
5 |
6 |
play2 |
6 |
3 |
play3 |
10 |
4 |
play4 |
5 |
7 |
play5 |
在在线网站中进行Excel转换Json
在线JSON校验格式化工具(Be JSON)
复制你要转换的内容
JsonUtlity序列化
JsonUtlity是unity自带的用于解析Json的公共类,它可以将对象序列化为Json格式的字符串,也可以将json反序列化为类对象
在文件中存读字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| using System.IO; using UnityEngine;
namespace Json { public class SaveManager : MonoBehaviour { private void Start() { File.WriteAllText(Application.persistentDataPath+"/test.json","测试用的json文件"); print(Application.persistentDataPath); } } }
|
在指定路径文件中读取字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| using System.IO; using UnityEngine;
namespace Json { public class SaveManager : MonoBehaviour { private void Start() { var str = File.ReadAllText(Application.persistentDataPath + "/test.json"); print("load:"+str); } } }
|
类对象序列化为json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| using System; using System.Collections.Generic; using System.IO; using UnityEngine;
namespace Json { public class Data { public string _name; public float _f; [SerializeField]private bool _sex; public List<int> _ids; public Person _friend1; public Person _friend2; public Data(string name,float f,bool sex,List<int> ids,Person friend1,Person friend2) { _name = name; _f = f; _sex = sex; _ids = ids; _friend1 = friend1; _friend2 = friend2; } }
[Serializable] public class Person { public string _name; public int _age; public bool _sex;
public Person(string name,int age,bool sex) { _name = name; _age = age; _sex = sex; } } public class SaveManager : MonoBehaviour { private void Start() { var d1 = new Data("11",10.66f,true,new List<int>{1,2,3},new Person("123",12,false),null); var jsonStr = JsonUtility.ToJson(d1); File.WriteAllText(Application.persistentDataPath + "/test.json", jsonStr); } } }
|
1
| {"_name":"11","_f":10.65999984741211,"_sex":true,"_ids":[1,2,3],"_friend1":{"_name":"123","_age":12,"_sex":false},"_friend2":{"_name":"","_age":0,"_sex":false}}
|
Json序列化特点
- float序列化时看起来有一些误差
- 自定义类需要加[Serializable]特性才能序列化
- 私有变量需要加[SerializeField]特性才能序列化
- JsonUtility不支持字典
- JsonUtility存储null对象不会是null,而是默认的数据
JsonUtility反序列化
1 2 3 4 5 6 7 8 9
| private void Start() { var jsonStr = File.ReadAllText(Application.persistentDataPath + "/test.json"); var d2 = JsonUtility.FromJson<Data>(jsonStr); Debug.Log(d2._f); }
|
注意
如果Json中少了数据,读取到内存的时候不会报错
只能用这种方法读取对象,也就是说Json文件的内容一定会被”{}”括住
文本的编码需要是UTF-8,不然无法加载
LitJson序列化
LitJson是一个第三方的库,用于处理Json的序列化和反序列化,C#编写,可以很容易的嵌入到我们的代码中
Releases · LitJSON/litjson (github.com)
使用Lit进行序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| using System.Collections.Generic; using System.IO; using LitJson; using UnityEngine;
namespace Json { public class Data { public string _name; public float _f; public bool _sex; public List<int> _ids; public Dictionary<int, string> _dic; public Person _friend1; public Person _friend2; public Data(string name,float f,bool sex,List<int> ids,Dictionary<int,string> dic,Person friend1,Person friend2) { _name = name; _f = f; _sex = sex; _ids = ids; _dic = dic; _friend1 = friend1; _friend2 = friend2; } } public class Person { public string _name; public int _age; public bool _sex;
public Person(string name,int age,bool sex) { _name = name; _age = age; _sex = sex; } } public class SaveManager : MonoBehaviour { private void Start() { var d1 = new Data("litJson",5.3f,true,new List<int>{1,2,3},new Dictionary<int, string>{{1,"123"},{2,"www"}},new Person("123",12,false),null); var jsonStr = JsonMapper.ToJson(d1); File.WriteAllText(Application.persistentDataPath + "/test.json", jsonStr); } } }
|
1
| {"_name":"litJson","_f":5.3,"_sex":true,"_ids":[1,2,3],"_dic":{"1":"123","2":"www"},"_friend1":{"_name":"123","_age":12,"_sex":false},"_friend2":null}
|
Lit序列化特点
LitJson与序列化特性无关,[SerializeField],[Serializable]不生效
这表示LitJson不用后者就能序列化对象,但没有办法序列化私有对象
LitJson支持字典,建议字典的键都是字符串
Json存储对象可以是null
float序列化看起来也没有误差
Lit反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| using System.Collections.Generic; using System.IO; using LitJson; using UnityEngine;
namespace Json { public class Data { public string _name; public float _f; public bool _sex; public List<int> _ids; public Dictionary<string, string> _dic; public Person _friend1; public Person _friend2; public Data() { } public Data(string name,float f,bool sex,List<int> ids,Dictionary<string,string> dic,Person friend1,Person friend2) { _name = name; _f = f; _sex = sex; _ids = ids; _dic = dic; _friend1 = friend1; _friend2 = friend2; } } public class Person { public string _name; public int _age; public bool _sex;
public Person(){ } public Person(string name,int age,bool sex) { _name = name; _age = age; _sex = sex; } } public class SaveManager : MonoBehaviour { private void Start() { var jsonStr = File.ReadAllText(Application.persistentDataPath + "/test.json"); JsonData data = JsonMapper.ToObject(jsonStr); print(data["_name"]); Data data2 = JsonMapper.ToObject<Data>(jsonStr); print(data2._f); } } }
|
注意
- LitJson反序列化时,字典的键必须是string
- LitJson反序列化时需要无参构造函数,我们要保证无参构造的存在
- LitJson可以直接读取数据集合
- 文本的编码需要是UTF-8,不然无法加载