c# - Unity JSON 解析返回 Null
问题描述
我有这个从 JSON 返回数组的方法
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
和包装类:
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
这是 JSON 文件结构:
{
"Tiles":
[
{
"id": "air",
"name": "Air",
"itemID": "air",
"texture": {
"x": 0.0,
"y": 0.0
},
"properties": {
"hardness": 0,
"undestructible": true
}
},
...
]
}
而这个 Tile 类:
[Serializable]
public class Tile {
public string id;
public string name;
public string itemID;
public Vector2 texture;
public Properties properties;
}
[Serializable]
public class Properties {
public double hardness = 0;
public bool undestructible = false;
}
当我运行 FromJson 方法时,它为 Tile 类返回 null,但对于以下 JSON 结构,它可以工作:
{
"Items":
[
{
"id": "dirt",
"name": "Dirt",
"description": "Just some dirt.. might be useful some day."
},
...
]
}
解决方案
推荐阅读
- react-native - 将整个应用程序从 React Native 59.8 迁移到 60+
- android - Android Jetpack 导航异常“无法从当前目的地找到导航”
- python - 使用 DSL 构建世界时的 SyntaxError
- android - 无法在条件语句中获取 Future 布尔值
- ios - M1:在带有 M1 芯片和 Big Sur 的 Mac 上使用模拟器 iOS 13 或更早版本和 Xcode 12.3 崩溃
- ios - 如何以编程方式从双卡 iPhone 中选择哪个 sim 卡发送短信?
- python - 带有双下标和期望符号的python图中的乳胶格式字符串问题
- django - 覆盖 Django 权限并修改 auth_permission
- android-jetpack-compose - 行内的 fillMaxHeight()
- python - kivy 中调用函数的问题(用 Python 的帮助编码)-.kv 文件中的调用函数