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

Parsing JSON to multi-dimensional Array in Unity

Discussion in 'Scripting' started by canuckvisa, Jul 1, 2021.

  1. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    Hello! How can I parse this type of JSON file to arrays?
    {
    "template":[
    [1,1,1,1],
    [1,1,1,1],
    [1,1,1,1],
    [1,1,1,1]
    ],
    "parts":[
    [
    [1,1,1,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]
    ],
    [
    [0,0,0,0],
    [1,1,0,0],
    [1,0,0,0],
    [1,0,0,0]
    ],
    [
    [0,0,0,1],
    [0,0,1,1],
    [0,0,1,0],
    [0,0,0,0]
    ],
    [
    [0,0,0,0],
    [0,0,0,0],
    [0,1,0,1],
    [0,1,1,1]
    ]
    ]
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  3. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    thank you for the link, but it does not work. Unity's JSON utility works when there are no such arrays in the JSON file.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Oh yeah, that's never gonna be useful.

    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.