Here is my JSON file:
[
{
"name": "An item"
}
]
Here is my helper class:
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
Here is the class I am trying to make objects out of:
public class Item
{
public string name, desc;
}
This is where I am calling the helper class:
Item[] itemList = JsonHelper.FromJson<Item>(itemJson.text);
The problem is itemList
and wrapper.array
in JsonHelper
is null
.
I copied code directly from:
http://www.boxheadproductions.com.au/deserializing-top-level-arrays-in-json-with-unity/
and
https://forum.unity.com/threads/how-to-load-an-array-with-jsonutility.375735/
What am I doing wrong?