Search Unity

Getting data from JSON

Discussion in 'Scripting' started by jsull1, May 7, 2020.

  1. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Hello, my unity c# application is getting information from a google firebase, I've figure out how to write to it and almost have been able to read from it but the information is sent in as a JSON file that I'm having trouble pulling into a dictionary or anything I can properly use. My data is coming in like this:
    {"Character1":{"stats":"Spears"},"Character2":{"stats":"Sword"},"Character3":{"stats":"Sheild"}}

    if anyone knows how to make this into data I can understand and grab that would be incredible!
    Thank you.
     
  2. shion33

    shion33

    Joined:
    Oct 21, 2019
    Posts:
    14
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Unity's built-in JSON serializer has weird problems in certain situations. It might work for you, but you also might want to consider using a third-party one instead (such as the one from Newtonsoft).
     
  4. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    okay I'll check it out! what can my information turn into, a list of a class or a dictionary? I'm mainly unsure of what the goal type will be
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Usually with JSON there are two ways to structure any particular bit data: as a custom class or as a dictionary. As a custom class, the JSON you pasted above might look like:
    Code (csharp):
    1. [System.Serializable]
    2. public class PlayerDataCollection {
    3. public PlayerData Character1;
    4. public PlayerData Character2;
    5. public PlayerData Character3;
    6. }
    7.  
    8. [System.Serializable]
    9. public class PlayerData {
    10. public string stats;
    11. }
    12.  
    13. PlayerDataCollection loadedData = JsonSerializer.Deserialize<PlayerDataCollection>(yourJsonString);
    14. Debug.Log(loadedData.Character1.stats); //prints "Spears"
    The main downside here should be clear: if you add characters to the json file you'll have to keep adding Character4, Character5, etc. That's not a good thing.

    The other way to deserialize a list list the one shown is as a dictionary:
    Code (csharp):
    1. Dictionary<string, PlayerData> myCollection = JsonSerializer.Deserialize<Dictionary<string, PlayerData>>(yourJsonString);
    2. Debug.Log(myCollection["Character1"].stats); //prints "Spears"
    That will give you a dictionary you can access with a string key as shown. This is better. However, I'd argue that, if you can change the structure of the JSON file, there's a better way, which is to use an array:
    Code (csharp):
    1. //json
    2. { "Characters" :
    3. [ {"stats":"Spears"}, {"stats":"Sword"}, {"stats":"Shield"} ]
    4. }
    5.  
    6. [System.Serializable]
    7. public class PlayerDataCollection {
    8. public PlayerData[] Characters;
    9. }
    10. PlayerDataCollection loadedData = JsonSerializer.Deserialize<PlayerDataCollection>(yourJsonString);
    11. Debug.Log(loadedData.Characters[0].stats); //prints "Spears"
    I recommend this because your character list seems more like a list to me, as "Character1" is not a meaningful key name. If you're going to use the key as the character name ("Bob" rather than "Character1"), then disregard. But imagine that you later have 30 characters, and at some point you delete the 12th one, you don't want to be stuck either renaming the keys of numbers 13-30 or living with a gap in your numbers. With an array, you can just remove it.
     
    jsull1 likes this.
  6. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    121
    Beautiful! Thank you mate with this information I've got it working perfectly! You are the best