I'm trying to deserialize a JSON string following this question, but im only getting "Object reference not set to an instance of an object" at line 14 of MenuItemBrands.cs (Debug.log(...))
Can anyone help me?
brands.json
{"brands":[{"id":1,"name":"test1","logo_img":"test.jpg"},{"id":2,"name":"test2","logo_img":"test.jpg"}]}
Brands.cs
[System.Serializable]
public class Brand
{
public int id;
public string name;
public string logo_img;
}
JsonHelper.cs
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[System.Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
MenuItemBrands.cs
public class MenuItem3dBrands : MonoBehaviour
{
private void Start()
{
LoadBrands();
}
protected void LoadBrands()
{
Brand[] brand = JsonHelper.FromJson<Brand>(Resources.Load<TextAsset>("brands").text);
Debug.Log(brand[0].id);
}
}