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 help parsing json object

Discussion in 'Scripting' started by k123m, Aug 22, 2023.

  1. k123m

    k123m

    Joined:
    Aug 15, 2023
    Posts:
    13
    hi im trying to parse json obj using JsonConvert.DeserializeObject<>() function
    i have created a class to map my json object to , also my json obj is in a c# sharp file that i import
    in a class that manage my json database

    the problem is when i deserialized the json object to my class and used it used it outside of class i get
    the key of the json object but the value is null

    her is my code:
    Code (CSharp):
    1. namespace DigimonsDex
    2. {
    3.     public class DigiDex
    4.     {
    5.         private Dictionary<string,Digimon> digimons;
    6.         private string digimons_json;
    7.         public DigiDex()
    8.         {
    9.             var jsonSettings = new JsonSerializerSettings()
    10.             {
    11.                 MissingMemberHandling = MissingMemberHandling.Error
    12.             };
    13.             digimons_json = Digimon_database.generateDigimonJsonData();
    14.             string jsonResult = JsonConvert.DeserializeObject(digimons_json).ToString();
    15.             digimons = JsonConvert.DeserializeObject<Dictionary<string, Digimon>>(jsonResult,jsonSettings);
    16.            
    17.         }
    18.  
    19.         public Digimon getDigimonByName(string digimon_name)
    20.         {
    21.             return this.digimons[digimon_name];
    22.         }
    23.  
    24.         public Dictionary<string, Digimon> getallDigimons()
    25.         {  
    26.             return digimons;
    27.         }
    28.     }
    29. }
    my class that i use for storying the json obj:
    Code (CSharp):
    1.     public class Digimon
    2.     {
    3.     public int num { get; set; }
    4.     public string name { get; set; }
    5.     public string gender { get; set; }
    6.     public string stage { get; set; }
    7.     public string type { get; set; }
    8.     public List<string> attribute { get; set; }
    9.     public BaseStats baseStats { get; set; }
    10.     public List<string> abilities { get; set; }
    11.     public string color { get; set; }
    12.     public List<string> eggGroups { get; set; }
    13.     public int memoryusage { get; set; }
    14. }
    15.  
    16. public class BaseStats
    17. {
    18.     public int hp { get; set; }
    19.     public int atk { get; set; }
    20.     public int def { get; set; }
    21.     public int spa { get; set; }
    22.     public int spd { get; set; }
    23.     public int spe { get; set; }
    24. }
    25.  
    the error im getting:
    NullReferenceException: Object reference not set to an instance of an object
    logic.getDigimonSpeed (System.String digimon_name) (at Assets/logic.cs:230)
    logic.setTurnBar (System.Collections.Generic.List`1[T] digimonlist) (at Assets/logic.cs:238)
    SceneLogic.Start () (at Assets/SceneLogic.cs:22)


    pleas advise me regarding this issue
    thanks in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    This has nothing to do with JSON. You're not initializing your collections.

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    IF you come to a problem with JSON, once you fix the trivia above, keep this in mind:

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
  3. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    569
    Which line is it erring on because the lines of code you provided do not match the lines in the error.
     
  4. k123m

    k123m

    Joined:
    Aug 15, 2023
    Posts:
    13
    the error pointing to a function that return the the class containing the data of the json

    Code (CSharp):
    1.  public  void setTurnBar(List<Digimon> digimonlist)
    2. {
    3.      string digimon_name = digimonlist[0].name;
    4.      string digimon2_name = digimonlist[1].name;
    5.      int digimon_speed = getDigimonSpeed(digimonlist[0].name.ToLower());
    6.      int digimon2_speed = getDigimonSpeed(digimonlist[1].name.ToLower());
    7.  
    8. }
    Code (CSharp):
    1.  public int getDigimonSpeed(string digimon_name)
    2. {
    3.      int digimon_speed = digimons[digimon_name].baseStats["spe"];
     
  5. k123m

    k123m

    Joined:
    Aug 15, 2023
    Posts:
    13
    for now i know where is the problem , i have a Logicmaneger object that mange my game logics , inside that game object I'm initiating the database manager class and initiate a variable containing my data base , for some reason when I print the variable inside start function I can see my object and its values , but inside other function in the Logicmaeger object script I get keys with null values?

    (the database class dos not extend MonoBehaviour class)
     
    Last edited: Aug 23, 2023
  6. k123m

    k123m

    Joined:
    Aug 15, 2023
    Posts:
    13
    problem solved , it seems I needed to initiate my object in Awake() function to initiate my object before scene is started
    what case my issue is that I was using the variable contain my data before it is initiated because its waiting for the scene to start to initiate it