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 How to save this values in JSON

Discussion in 'Scripting' started by VengarlofForossa, Aug 4, 2023.

  1. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    EDIT: When i fixed the problem, i delete this edit.

    Hello Everyone!

    I need help storing information in a JSON, as I am quite inexperienced in the subject.

    When my game first starts, I create a JSON file that is empty. When it gets to the main menu, I check if the JSON is empty or not.

    If it is empty, it opens a panel where you enter your name to save that information in the JSON.

    In the panel, the player enters his name and presses the create button. I have created a function that gets the name entered by the user.

    All this works correctly.

    I have a String variable with the name and a DateTime with the creation date. But how do I store that information in the JSON? I have no idea

    Code (CSharp):
    1. public void createProfile()
    2.     {
    3.         string namePlayer;
    4.         namePlayer = nameInputField.text;
    5.         DateTime currentDateTime = DateTime.Now;
    6.  
    7.         //HOW TO DO ?
    8.         string fileJSON = JsonUtility.ToJson(namePlayer);
    9.         System.IO.File.WriteAllText(Application.persistentDataPath + "/playerData/profileGame.json", fileJSON);
    10.  
    11.         Debug.Log(namePlayer);
    12.         Debug.Log(currentDateTime.ToString());
    13.     }
    14.  
    I know how JSON works, but not so much in C#.
    The idea is to have a structure like this

    Code (CSharp):
    1. {
    2.   "name": "Vengarl",
    3.   "lastSave": "2023...",
    4.   "object": {
    5.     "a": "b",
    6.     "c": "d"
    7.   }
    8. }
     
    Last edited: Aug 4, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Then do it! What you want is these sites:

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

    to get started, but then look at the generated C# class and make sure it is what you want (string, etc.)

    If anything is
    object
    you probably want to change it to your correct data type.

    Then you simply serialize/deserialize the entire structure at once, dig into it, change it, write it back out.

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Finally...

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide