最近在项目中使用Json读取存储数据,本来想用Unity自带的JsonUtility的,但是无奈Unity自带的Json解析功能太少,比如不支持Dictionary等等。后来使用了另外一个JsonNet插件,比Unity好用很多很多,也很方便,性能也不错。
Newtonsoft.JsonNet大家可以自己到Github下载,提供了dll和源代码。GitHub地址:https://github.com/SaladLab/Json.Net.Unity3D/releases
现在要说的是碰到的一个坑,在ios打包到手机的时候运行报错,报错如下:
MissingMethodException: Method not found: 'Default constructor not found...ctor() of System.ComponentModel.TypeConverter'.
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
at System.ComponentModel.TypeDescriptor.GetConverter (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer) [0x00000] in <filename unknown>:0
最开始猜测是因为il2CPP的时候用的是dll而非源代码,后面百度下了,有位仁兄也碰到了,猜测是因为Unity代码优化把没用到的类删除了,所以导致出错了...
在JsonNet官方的下载包里面找到了README,查看了,找到了解决方案。
## FAQ
- Q: App stops throwing MissingMethodException for ComponentModel.TypeConverter like this.
```
MissingMethodException: Method not found:
'Default constructor not found...ctor() of System.ComponentModel.TypeConverter'.
```
- A: link.xml should be added to your project.
也就是把下载包中的link.xml拉到项目中任何一个地方即可。