我有这个从JSON返回数组的方法
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
Wrapper课程:
[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."
},
...
]
}