Search Unity

JSON loading arry

Discussion in 'Scripting' started by TheRaider, Nov 16, 2020.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I have been trying to make a sample of loading in JSON array and can't seem to get it working. I get no errors but I can't seem to get any data,

    The JSON file
    Code (CSharp):
    1. {
    2.     "Player": [
    3.         {
    4.             "name": "Warrior",
    5.             "health": 100,
    6.             "mana": 20
    7.         },
    8.         {
    9.             "name": "Wizard",
    10.             "health": 40,
    11.             "mana": 80
    12.         },
    13.         {
    14.             "name": "Healer",
    15.             "health": 100,
    16.             "mana": 120
    17.         }
    18.     ]
    19. }
    and here is the code I am using. I also left a few things I tried in and commented out
    Code (CSharp):
    1.     public TextAsset textJSON;
    2.  
    3.  
    4.     [System.Serializable]
    5.     public class Player
    6.     {
    7.  
    8.         public string name;
    9.         public string health;
    10.         public int mana;
    11.     }
    12.     [System.Serializable]
    13.     public class PlayerList
    14.     {
    15.  
    16.         public Player[] player;
    17.     }
    18.  
    19.     public PlayerList myPlayerList = new PlayerList();
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         myPlayerList = JsonUtility.FromJson<PlayerList>(textJSON.text);
    25.         //myPlayerList = (PlayerList)JsonUtility.FromJson(textJSON.text,typeof(PlayerList));
    26.         //JsonUtility.FromJsonOverwrite(textJSON.text, myPlayerList);
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Case sensitivity issue detected ^^

    Your json text reads:
    Code (CSharp):
    1. {
    2.     "Player": [
    3.         {
    while the class you created to represent your root object reads

    Code (CSharp):
    1.  public Player[] player;
    "player" and "Player" are not the same thing.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    What Bunny says above... I also 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. Instead grab Newtonsoft JSON .NET off the asset store for free, as it is way better.

    PS: for folks howling about how it 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.
     
  4. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    It was simply a lack of understanding on my part. I thought it was meant to be Player with the cap P. Changing to lower case and it works fine.

    Honestly I was just curious how to do it since I often just read in text files (like I find a file online with 1000 names and read it in to apply to NPC's etc). Some said why don't you use JSON, so I thought I would give it a go!

    The silent fail did suck!
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    This is the best attitude to have as an engineer!!
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    Thanks :) but I am more of a designer who likes to be able to make things by myself.

    Still a bit embarrassed how simple the issue was, but glad I asked because with no error I would have never got this.

    I had a look at that asset, it looks like a direct replacement using the same namespace which is awesome!