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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to produce JSON file with C#, then read it back?

Discussion in 'Editor & General Support' started by marci8500, Jul 5, 2020.

  1. marci8500

    marci8500

    Joined:
    Jan 13, 2020
    Posts:
    6
    Hello!
    So I have a C# script which literally does nothing, and I can't figure out why.
    Code (CSharp):
    1. public TextAsset jsonFile;
    2.     Item item = new Item();
    3.     public string contents;
    4.  
    5.     void Start()
    6.     {
    7.         item.coin = new ItemData();
    8.         item.coin.chance = 20.0f;
    9.         item.coin.minDepth = 0.2f;
    10.         item.coin.maxDepth = 6.7f;
    11.  
    12.         contents = JsonUtility.ToJson(item, true); // contents = {}
    13.         Debug.Log("contents: " + contents); //contents: {}
    14.  
    15.  
    16.         loadData();
    17.     }
    18.  
    19.     void loadData()
    20.     {
    21.         item = JsonUtility.FromJson<Item>(contents);
    22.  
    23.         Debug.Log("loadData: " + item.coin.chance + " " + item.coin.minDepth + " " + item.coin.maxDepth); //NullReferenceException: Object reference not set to an instance of an object
    24.     }
    25.  
    26.     [System.Serializable]
    27.     public class ItemData
    28.     {
    29.         public float chance { get; set; }
    30.         public float minDepth { get; set; }
    31.         public float maxDepth { get; set; }
    32.     }
    33.  
    34.     public class Item
    35.     {
    36.         public ItemData coin { get; set; }
    37.     }
    And my desired JSON output is:
    Code (JavaScript):
    1. {
    2.     "coin":{
    3.         "chance": 20.0,
    4.         "mindepth": 0.2,
    5.         "maxDepth": 6.7
    6.     },
    7.     "coinSmall":{
    8.         "chance": 40.0,
    9.         "mindepth": 0.2,
    10.         "maxDepth": 6.7
    11.     },
    12.     "coinBig":{
    13.         "chance": 40.0,
    14.         "mindepth": 3.2,
    15.         "maxDepth": 9.5
    16.     }
    17.     // Much more coin.
    18. }
    19.  
    So, how could I achieve it? I'm not familiar with C#, I'm learning it now. In python, and Java I can do it, but in C# OMG I can't.
    Please help me!!
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    You should be more clear on the problem. You specifically said this code "literally does nothing", but then I see in the comments of your code that the attempt to call Debug.Log is producing a NullReferenceException?

    Anyway, you haven't shown the code for Item, but the most likely problem is that you haven't made the class serializable. Look into the Serializable attribute. Make sure that "contents" on line 12 ends up having the string value you expect, otherwise there's obviously no reason to try to load an empty string.
     
  3. marci8500

    marci8500

    Joined:
    Jan 13, 2020
    Posts:
    6
    Thanks! You helped me even with this kind of small clarification!