Unity中使用json序列化失敗
阿新 • • 發佈:2018-01-22
pan () 編輯器 double類型 tor pub 引號 div 鍵值
- 問題
//為什麽用json序列化這樣沒得問題 Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("1", 1); string strJson = LitJson.JsonMapper.ToJson(dic); //這樣有問題 Dictionary<int, int> dic2 = new Dictionary<int, int>(); dic2.Add(1, 1); string strJson2 = LitJson.JsonMapper.ToJson(dic2); Debug.Log(strJson2);
- 解決方法
json 序列化時不支持結構體,比如Unity 中的Vector3類型不支持,所以我們要自己轉型以下
1 //Vector3 裏面原來是float類型,但是 json 不支持float類型,所以用 double類型 2 public class Vector3Obj 3 { 4 double x; 5 double y; 6 double z; 7 }
- 使用json的註意事項
- - JSON字符串裏的非數字型鍵值沒有雙引號
- - JSON中存在\t這樣的制表符,看起來和空格一樣,但是就是因為它存在校驗不通過,需要去掉
- - 編輯器有bom頭也會造成
Unity中使用json序列化失敗