How to save this values in JSON

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

public void createProfile()
    {
        string namePlayer;
        namePlayer = nameInputField.text;
        DateTime currentDateTime = DateTime.Now;

        //HOW TO DO ?
        string fileJSON = JsonUtility.ToJson(namePlayer);
        System.IO.File.WriteAllText(Application.persistentDataPath + "/playerData/profileGame.json", fileJSON);
  
        Debug.Log(namePlayer);
        Debug.Log(currentDateTime.ToString());
    }

I know how JSON works, but not so much in C#.
The idea is to have a structure like this

{
  "name": "Vengarl",
  "lastSave": "2023...",
  "object": {
    "a": "b",
    "c": "d"
  }
}

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://discussions.unity.com/t/799896/4

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

https://discussions.unity.com/t/870022/6

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

https://discussions.unity.com/t/892140/8

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() on a GameObject instance, or use ScriptableObject.CreateInstance() 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

2 Likes

Do you know if there is possibility to save entire Components / Vector3s / Quaternions into json or only basic variables?

You can construct your own test faster than I can type a full response.

GO!

EDIT: note also the answer is already explained above, so make sure you read posts before adding to them. :slight_smile:

2 Likes