当前位置: 代码迷 >> 综合 >> Unity 使用JsonMapper过程中遇到的问题
  详细解决方案

Unity 使用JsonMapper过程中遇到的问题

热度:64   发布时间:2023-09-21 22:42:29.0

我在使用T[] datas = JsonMapper.ToObject<T[]>(conntent);时因为我的Json中有int数据,所以一直报错。

JsonException: Can't assign value '1' (type System.Int32) to type System.String

因为JsonMapper默认不能将int转成string 

这里需要我们动态修改数据格式

JsonMapper.RegisterImporter<int, string>((int value) =>{return value.ToString();});

在我们使用JsonMapper.ToObject前加上上面代码就可以。

同理,float/double/int/string 这些数据类型之间都可以相互转换