問題:
//為什么用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 序列化時(shí)不支持結(jié)構(gòu)體,比如Unity 中的Vector3類型不支持,所以我們要自己轉(zhuǎn)型以下
//Vector3 里面原來是float類型,但是 json 不支持float類型,所以用 double類型
public class Vector3Obj
{
double x;
double y;
double z;
}
使用json的注意事項(xiàng):
- JSON字符串里的非數(shù)字型鍵值沒有雙引號(hào)
- JSON中存在\t這樣的制表符,看起來和空格一樣,但是就是因?yàn)樗嬖谛r?yàn)不通過,需要去掉
- 編輯器有bom頭也會(huì)造成