1 2 3 4 5 6 7 8 9
| ### LitJosn
`using LitJson` 引入命名空间
#### 序列化
```csharp string Json = JsonMapper.ToJson(lineList); File.WriteAllText(Application.persistentDataPath + "/save.json", Json);
|
lineList
是要序列化的对象
反序列化
1 2
| string data = File.ReadAllText(Application.persistentDataPath + "/save.json"); List<List<int>> Data = JsonMapper.ToObject<List<List<int>>>(data);
|
lineList
是什么类型就用什么类型接收,例如这里用 List<List<int>>
接收
JsonUnity
1 2 3 4 5 6 7 8 9 10 11 12 13
| private void Save() { StartData startData = new StartData(); string json = JsonUtility.ToJson(startData); File.WriteAllText(Application.persistentDataPath + "/StartData.json", json); }
private void Read() { string json = File.ReadAllText(Application.persistentDataPath + "/StartData.json"); StartData startData = JsonUtility.FromJson<StartData>(json); }
|
注意:
- float 序列化可能存在微小误差(不影响使用)
- 自定义类需添加序列化标记
[System.Serializable]
- 序列化私有变量需添加
[SerializeField]
- 不支持字典类型
- 存储的 null 对象会被转换为默认类型值