Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug JsonUtility.FromJson not binding json to properties.

Discussion in 'Scripting' started by jarosgregory, May 21, 2022.

  1. jarosgregory

    jarosgregory

    Joined:
    May 9, 2022
    Posts:
    17
    I've used Newtonsoft's JsonSerializer and had no problems with it in the past. Getting this serializer to work though is driving me mad. I made this project to try and narrow the issue as much as I could. In the attached image you can see that:
    • The model I'm using is seralizable.
    • The path name and file name are correct.
    • The json string and C# model I'm using match perfectly.
    I really am at a loss. I've tried for 3 days for a couple hours at a time to figure out what is going wrong. I'm sure it's something simple but a fresh pair of eyes would help. The code and json string you see in the image is below. Thank you all in advance for your help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3.  
    4. public class Thing : MonoBehaviour
    5. {
    6.     private ThingModel Model;
    7.     private string jsonString;
    8.  
    9.     private void Awake()
    10.     {
    11.         jsonString = File.ReadAllText(".\\Assets\\values.json");
    12.         Model = JsonUtility.FromJson<ThingModel>(jsonString);
    13.         print(jsonString);
    14.         print($"Name: {Model.Name}, Number: {Model.Number}, Color: {Model.Color}, IsSquare: {Model.IsSquare}");
    15.     }
    16. }
    17.  
    18. [System.Serializable]
    19. public class ThingModel
    20. {
    21.     public string Name { get; set; }
    22.     public string Color { get; set; }
    23.     public int Number { get; set; }
    24.     public bool IsSquare { get; set; }
    25. }
    26.  
    { "Name": "Greg", "Color": "Green", "Number": 12, "IsSquare": true }
     

    Attached Files:

    • wtf.PNG
      wtf.PNG
      File size:
      172.8 KB
      Views:
      247
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    May I ask what this has to do with Unity Hub? This is the forum you posted this on btw.
     
  3. jarosgregory

    jarosgregory

    Joined:
    May 9, 2022
    Posts:
    17
    Nothing, just a mistake. I just saw "help and questions" and made my post. I'll pay more attention next time.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    No problem. I've moved your post to the scripting forum so hopefully you'll get the help you need here. :)
     
    jarosgregory likes this.
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091
    Unity's JsonUtility is very basic. as far as I know it doesn't serialize / deserialize properties. i.e.
    public int FieldName {get; set;}

    Remove the
    {get; set;}


    There are quite some differences between JsonUtility and NewtonsoftJson.
    If you need anything more than JsonUtility supports, use NewtonsoftJson.

    https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@3.0/manual/index.html
    PackageManager -> + -> Add by package name ->
    com.unity.nuget.newtonsoft-json



    Code (CSharp):
    1. [System.Serializable]
    2. public class ThingModel
    3. {
    4.     public string Name;
    5.     public string Color;
    6.     public int Number;
    7.     public bool IsSquare;
    8. }
     
    CrandellWS and jarosgregory like this.
  6. jarosgregory

    jarosgregory

    Joined:
    May 9, 2022
    Posts:
    17
    That did it! Thank you for the help. I assumed that the two were the same, just one somehow better for Unity.
     
  7. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091
    They made it easy with the JsonUtility, but it is a really stripped down version of it. The bare minimum, due to this it has quite some restrictions.
    Not being able to serialize properties being one of them.