Search Unity

Question Write/Read array of arrays in JSON files

Discussion in 'Scripting' started by Digitex1, Jun 18, 2021.

  1. Digitex1

    Digitex1

    Joined:
    Aug 28, 2017
    Posts:
    42
    Hello,

    I'm trying to read a JSON file that contains an array of arrays like this:
    Code (CSharp):
    1. "notes":[
    2.          {
    3.             "mustHitSection":false,
    4.             "typeOfSection":0,
    5.             "lengthInSteps":16,
    6.             "sectionNotes":[
    7.                [
    8.                   0,
    9.                   2,
    10.                   0
    11.                ],
    12.                [
    13.                   600,
    14.                   3,
    15.                   450
    16.                ],
    17.                [
    18.                   1050,
    19.                   3,
    20.                   600
    21.                ]
    22.             ]
    23.          }
    to do so, I need to implement a class that has the same structure in order to be able to use JsonUtilitiy.ReadFromJson<T>.

    So, the variable "notes" is an array of a class with variables: "mustHitSection" / "typeOfSection" / "lengthInSteps" & "ectionNotes".

    I've implement the last variable "sectionNotes" as an array of array:
    Code (CSharp):
    1. public bool mustHitSection;
    2.     public float typeOfSection;
    3.     public float lengthInSteps;
    4.     public float[][] sectionNotes;
    5.  
    6.     public songNotes(bool hit, float typeSec, float lengto, float[][] section)
    7.     {
    8.         mustHitSection = hit;
    9.         typeOfSection = typeSec;
    10.         lengthInSteps = lengto;
    11.         sectionNotes = section;
    12.     }
    I'm using JsonUtility to generate a test file:
    Code (CSharp):
    1. string filePath = Path.Combine(Application.persistentDataPath, "DataGenTest.json");
    2.         string jsonData = JsonUtility.ToJson(theSong, true);
    3.         File.WriteAllText(filePath, jsonData);
    However, in the results: I find that the variable "sectionNotes" is completely missing in the generated JSON file:
    Code (CSharp):
    1. "notes": [
    2.             {
    3.                 "mustHitSection": false,
    4.                 "typeOfSection": 0.0,
    5.                 "lengthInSteps": 16.0
    6.             },
    7.             {
    8.                 "mustHitSection": false,
    9.                 "typeOfSection": 0.0,
    10.                 "lengthInSteps": 16.0
    11.             }
    12.         ]
    What am I doing wrong? :rolleyes:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Your only sin is assuming that Unity's JSON is a reasonable choice of tool.

    That is an understandable oversight and we all have been there and wasted our lives on it.

    Problems with Unity "tiny lite" built-in JSON:

    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 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://forum.unity.com/threads/jso...-not-working-as-expected.722783/#post-4824743

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

    PS: for folks howling about how it will "add too much size" to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.
     
    Last edited: Jun 18, 2021
  3. Digitex1

    Digitex1

    Joined:
    Aug 28, 2017
    Posts:
    42
    Thank you, it worked perfectly!
     
    Kurt-Dekker likes this.