Search Unity

Question JSON parser into jagged Array

Discussion in 'Scripting' started by OuroborosDraconis, Dec 6, 2022.

  1. OuroborosDraconis

    OuroborosDraconis

    Joined:
    Oct 4, 2022
    Posts:
    49
    Hi,
    I am trying to read in a JSON file into a jagged Array.
    simplified it looks like this in my class:

    ...
    public float[ ][ ][ ] jagArr = new float [3][ ][ ]
    {
    new float[2][ ] { new float[100], new float[200] },
    new float[2][ ] { new float[100], new float[200] },
    new float[2][ ] { new float[100], new float[200] }
    };
    ...

    Since there are no errors and its possible to run the game with that script I believe that I made the initialization of the jagged array correctly. But since the JsonUtility seems to stop reading in the data from my JSON file when it reaches the jagged array, I believe that the problem is in the JsonUtility.
    After the reading there is not even shown an empty jagged array when using [System.Serializable], there is just nothing in the inspector. That speaks a bit against my theory.

    can anybody help me with some good hints how to solve that?

    this guys says its not possible to read in with JsonUtility a jagged array, does anybody know a solution for that problem? Unfortunatly his link to his simple parser does not work anymore.

    https://answers.unity.com/questions/1322769/parsing-nested-arrays-with-jsonutility.html
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Be sure to leverage sites like:

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

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

    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 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
     
  3. OuroborosDraconis

    OuroborosDraconis

    Joined:
    Oct 4, 2022
    Posts:
    49
    actually I am more certain that my jagged array is being correctly initialized since I receive output that depends on this elements. if the initialization would not work then there wouldn't be zero values. So it realy must be the parser that is not reading in the data, leaving all entries of my jagged array zero..?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Probably. See my note and stay tf away from Unity's JSON code.
     
  5. OuroborosDraconis

    OuroborosDraconis

    Joined:
    Oct 4, 2022
    Posts:
    49
    Thanks for your Help Kurt, but now I have the probelm of having two newtonsoft.dll installed. and that even I checked first in the package manager if there is anykind of JSONwhatever mentioned. I guess thats not a problem I'm gonna solve today...
    thanks for your help and have a good night