Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question JsonUtility.FromJson Error converting a internal object

Discussion in 'Scripting' started by jmgc92, Mar 8, 2022.

  1. jmgc92

    jmgc92

    Joined:
    Apr 3, 2016
    Posts:
    9
    Hi, im having problem to use JsonUtility.FromJson the internal object is always getting the default value.

    My objects:

    Code (CSharp):
    1. [SerializeField]
    2.    public class MessageType
    3.    {
    4.        public string id;
    5.    }
    6.  
    7.    [SerializeField]
    8.    public class InitMessageData : MessageType
    9.    {
    10.        public GameData payload;
    11.    }
    12.  
    13. [SerializeField]
    14.    public struct GameData
    15.    {
    16.        public string gameName;
    17.        public string assetsPath;
    18.        public string locale;
    19.        public bool kickoutEnabled;
    20.        public int kickoutTime;
    21.        public bool firstTimePlaying;
    22.        public long increaseProgress;
    23.        public long initialProgress;
    24.        public bool pathGameStep;
    25.        public bool waitForPreloaderEvent;
    26.        public bool hasSound;
    27.        public string environment;
    28.        public GameFeatureData features;
    29.    }
    30.  
    31.    [SerializeField]
    32.    public struct GameFeatureData
    33.    {
    34.        public bool cheerings;
    35.        public bool email;
    36.        public bool parentsInOnboarding;
    37.        public bool pricingOverTime;
    38.        public bool trialTimes;
    39.    }

    My JSON string:

    Code (CSharp):
    1. string messageFromBrowser ="{\"id\":\"init\",\"payload\":{\"assetsPath\":\"URL\",\"firstTimePlaying\":false,\"gameName\":\"miceandcoding\",\"hasSound\":false,\"kickoutEnabled\":true,\"pathGameStep\":false,\"kickoutTime\":60}}";
    My execution code:
    Code (CSharp):
    1. InitMessageData initMessage = JsonUtility.FromJson<InitMessageData>(messageFromBrowser);
    The result:
    In the attached screedshot



    For example if i try casting directly to the GameData object using this string json (is the same but removing the id and payload container)

    Code (CSharp):
    1. string messageFromBrowser ="{\"assetsPath\":\"URL\",\"firstTimePlaying\":false,\"gameName\":\"miceandcoding\",\"hasSound\":false,\"kickoutEnabled\":true,\"pathGameStep\":false,\"kickoutTime\":60}";

    Code (CSharp):
    1. GameData initMessage = JsonUtility.FromJson<GameData>(messageFromBrowser);
    This work and i get the full data
     

    Attached Files:

  2. jmgc92

    jmgc92

    Joined:
    Apr 3, 2016
    Posts:
    9
    UPDATE

    I have add the library newtonsoft to use JsonConvert, using the same JSON string.

    Code (CSharp):
    1. InitMessageData initMessage = JsonConvert.DeserializeObject<InitMessageData>(messageFromBrowser);
    And this work, im compiling from WebGL so im trying to not add more library in order to have a lighweight build, any idea how can be this solve using JsonUtility.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    You wrongly applied the "SerializeField" attribute to your types. As the name of the attribute suggests it is supposed to be applied to a field, not a type. You have to use the "System.Serializable" attribute on your types to make them serializable.