c# - Unity.FromJason 解析时无效值
问题描述
当试图从 Json 中反序列化数据时,我得到一个无效的值错误。为了进行实验,我减少了键/值的数量。我尽力找到解决方案,但我从未使用过 json。
{"id": 2} //all code inside my json
public class Weapon
{public int id;}
Debug.Log(JsonUtility.FromJson<Weapon>("/Resources/db/wargear.json"));
ArgumentException: JSON parse error: Invalid value.
解决方案
您尝试解析的 JSON 是文字路径"/Resources/db/wargear.json"
。
您必须首先将 JSON 文件作为文本资产加载,并将其中的文本传递给 JSON 反序列化器函数。
public class Weapon
{
public int id;
}
void Start()
{
var textAsset = Resources.Load<TextAsset>("db/wargear.json");
var weapon = JsonUtility.FromJson<Weapon>(textAsset.text);
Debug.Log(weapon.id);
}
推荐阅读
- python - 我的 django 梦幻足球项目。如何将其他用户的分数显示给每个用户?
- javascript - 从 api axios Vue.js 获取数据时出现问题“无效的道具:道具“项目”的类型检查失败。预期的数组,函数,得到对象”
- kotlin - 有没有更优雅的方法来检查字符串是否是 Kotlin 中的有效 Int?
- html - 使用包含的 Sass 动画混合
- python - 用水量追踪器无法添加数字
- android - Firebase - 如何在 android 应用中获取归因
- symfony - Doctrine - 与关联实体的验证
- reactjs - 有没有比 redux persist 更好的方法来为生产环境持久化 redux 状态数据
- python - 背包问题
- android - chekbox 总是检查 recyclerview 中的最后一项