Search Unity

JsonUtility FromJson returns null

Discussion in 'Scripting' started by gk104, May 26, 2017.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    i having an issue deserializing some info from a json file, I never used Json nor JsonUtility class before, and I have no clue wheres the issue, just trying to read the json file and put it into my object that stores all saved data,

    but FromJson keep returning null, what did I do wrong?

    Code (CSharp):
    1. public class MyPlayer : MonoBehaviour{
    2.  
    3.     public PlayerInfo mPlayer; // Object that stores the the saved data
    4.  
    5.     string path;
    6.     string JsonString;
    7.  
    8.     private void Start()
    9.     {
    10.         path = Application.streamingAssetsPath + "/PlayerInfo.json";
    11.         JsonString = File.ReadAllText(path);
    12.     }
    13.  
    14.     public void LoadDataFromJson() // Called when "Continue" button gets pressed In the Login Menu
    15.     {
    16.         mPlayer = JsonUtility.FromJson<PlayerInfo>(JsonString); // just puts the data from the json file into this object
    17.         Debug.Log(mPlayer);
    18.     }
    19. }
    20.  
    21. [Serializable]
    22. public class PlayerInfo {
    23.  
    24.     public string userName;
    25.     public int Level;
    26.     public int Coins;
    27.     public int Health;
    28.  
    29.     public string equippedSword;
    30.     public string equippedGun;
    31.  
    32.     public int swordCount;
    33.     public int gunCount;
    34.  
    35.     public List<string> mySwords = new List<string>();
    36.     public List<string> myGuns = new List<string>();
    37.  
    38.     public PlayerInfo(string userName, int Level, int Coins, int Health, string equippedSword, string equippedGun,
    39.         int swordCount, int gunCount)
    40.     {
    41.         this.userName = userName;
    42.         this.Level = Level;
    43.         this.Coins = Coins;
    44.         this.Health = Health;
    45.         this.equippedSword = equippedSword;
    46.         this.equippedGun = equippedGun;
    47.         this.swordCount = swordCount;
    48.         this.gunCount = gunCount;
    49.  
    50.         mySwords.Add(equippedSword);
    51.         myGuns.Add(equippedGun);
    52.     }
    53. }
    54.  
    This is the Json File im trying to read:
    PlayerInfo.json
    Code (CSharp):
    1. {
    2.     "userName": "Test",
    3.     "Level": "Test",
    4.     "Coins": "Test",
    5.     "Health": "Test",
    6.  
    7.     "swordCount": "Test",
    8.     "gunCount": "Test",
    9.  
    10.     "equippedSword": "Test",
    11.     "equippedGun": "Test",
    12. }
     
  2. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    I've set JsonString to public, for seeing it in the inspector, it only shows me:" { "
    seems like it's only reading the first line from the json file, but why? shouldn't File.ReadAllText read all lines from the file?
     
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    I haven't done this before, but just looking at the documentation, I have one question for you...

    Do you have a StreamingAssets path in your project? I'm not sure if that path goes in your Assets folder, or the root.. I think the former.. then you drop the JSON file inside that StreamingAssets folder. I think if you just put the JSON right in the root of your project, it isn't really looking there.

    Outside of that stab in the dark, do you need to wrap your json file with a PlayerInfo section first maybe? been a while since i've worked with JSON

    Edit: answering my own question, I don't think you have to wrap it inside "PlayerInfo"

    i.e.
    Code (csharp):
    1.  
    2. {"PlayerInfo": {
    3.        "userName": "Test",
    4.        "Level": "Test",
    5.        "Coins": "Test",
    6.        "Health": "Test",
    7.  
    8.        "swordCount": "Test",
    9.        "gunCount": "Test",
    10.  
    11.        "equippedSword": "Test",
    12.        "equippedGun": "Test"
    13.     }
    14. }
    15.  
    Edit 2: my post is likely useless. haha, but figured I'd try and help.
     
    Last edited: May 26, 2017
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    In response to your second post, that may not show up in the inspector properly but still be read properly.
    You could try:
    Code (csharp):
    1.  Debug.Log("String length = " + JsonString.Length.ToString());
     
  5. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    Its already in the StreamingAssets folder, and i've checked again, and it indeed goes into into JsonString,
    checked again the JsonString in the Inspector, and it's already contains everything from the json file, I just did not see it because it contains a lot of empty lines.

    it's kinda weird.. i've also tried to add this line:
    Debug.Log("String length = " + JsonString.Length.ToString())

    but it's throws a null exception, "object reference not set to an instance of an object", hows that even possible that JsonString is empty?
     
    Last edited: May 26, 2017
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you found it so that means using the debug isn't needed anymore, though I have no idea how you have a null ref there. Sorry, I'm not seeing what the problem is.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    One thing is your json string is incorrect. You have level (and other variables) declared as an int in your class, but as a string in your json file. That will need to be fixed. This could be why you're getting null. FromJson may try to cast it, be unable to, and thus return null.