Spawning Objects according to data at JSON Matrix

Hello everyone, I’m trying to spawn my game objects according to this JSON. Unfortunately, I couldn’t make this happen, I’m stucked. Can someone help me to achieve this?
{ “_matrix”: [
1, 0, 1, 2, 1,
0, 1, 0, 1, 2,
2, 2, 1, 2, 1,
1, 1, 2, 1, 0,
0, 2, 0, 0, 1,
2, 0, 2, 0, 1 ]}

  • at 0 there will be cherry,
  • at 1 there will be banana,
  • and at 2 there will be watermelon.

This is my JSONReader Class:

[Serializable]
    public class JSONReader
    {
        public List<int> _matrix;
  
        private void Awake()
        {
            _matrix = new List<int>();
            LoadItem("Assets/Resources/_matrix.json");
        }
    
        public void LoadItem(string path)
        {
            string myText = File.ReadAllText(path);
            _matrix = JsonUtility.FromJson<List<int>>(myText);
        }

and This is my Grid Generator Class;

Using System.Collections.Generic;
    using UnityEngine;
    using Random = UnityEngine.Random;
  
    public class GridGenerator : MonoBehaviour
    {
        public int height = 6;
        public int width = 5;
        private float GridSpaceSize = 1.5f;
        public List<GameObject> foods;
        private GameObject[,] _gameGrid;
  
        private void Awake()
        {
            CreateGrid();
            Debug.Log(FillTheMatrix.LoadItem());
        }
      
        private void CreateGrid()
        {
            _gameGrid = new GameObject[width, height];
            List<int> _matrix = FillTheMatrix.LoadItem();
            if (foods == null)
            {
                Debug.LogError("ERROR: Grid Cell Prefab on the Game grid is not assigned");
            }
                // TO DO 3 MATCH GAME
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    _gameGrid[x, y] = Instantiate(foods[Random.Range(0,3)], new Vector3(x * GridSpaceSize - 3, 0.5f, y * GridSpaceSize - 3),
                        Quaternion.identity);
                    _gameGrid[x, y].transform.parent = transform;
                    _gameGrid[x,y].gameObject.name = "Grid Space ( X: " + x.ToString() + " , Y: " + y.ToString() + ")";
                }
            }
        }
    }

This sounds terrible but unfortunately not really helpful.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://forum.unity.com/threads/jso…-not-working-as-expected.722783/#post-4824743

Also, always be sure to leverage sites like:

https://csharp2json.io

PS: for folks howling about how NewtonSoft JSON .NET will “add too much size” to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.