How to take a matrix (List<List<>>()) from a Json?

I have this class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Map
{
    public int widht;
    public int height;
    public List<List<string>> box;
    public List<List<bool>> exploited;
    public int num_diggers;

    public Map()
    {

    }
    public void printContent()
    {

    }

    public void actualizarDimensiones(Dimensiones dimension)
    {

    }
}

and I am trying to get the dataa from that JSON:

{β€œwidht”:10,
β€œheight”:10,
β€œbox”:[[β€œH”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œW”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œW”,β€œB”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œW”,β€œW”,β€œF”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œF”,β€œF”,β€œB”,β€œB”,β€œB”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œF”,β€œP”,β€œP”],[β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œP”,β€œM”,β€œM”],[β€œW”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œB”,β€œP”,β€œM”,β€œM”]],
β€œexploited”:[[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false],[false,false,false,false,false,false,false,false,false,false]],
β€œnum_diggers”:1,
β€œdiggers”:[{β€œgolds”:[ ],
β€œid”:β€œ0”,
β€œx”:1,
β€œy”:0}],
β€œgolds”:[ ]}

but I can’t take the values of β€œbox” and β€œexploited” witch are matrix. I am using the JsonUtility of Unity.

What does that means? What is shown in the debugger for that fields after deserialization? May be it will be of type int[ ][ ], not list<list> ?

JsonUtility can not handle nested Lists, but here is an example of what you can do to work around this issue :slight_smile:

3 Likes

oh thanks, I will try that!

Ok I changed my code and now looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Map
{
    public int widht;
    public int height;
    public List<StringListWrapper> box;
    public List<StringListWrapper> exploited;
    public int num_diggers;

    public Map()
    {

    }
    public void printContent()
    {
        Debug.Log("Width: " + widht);
        Debug.Log("Height: " + height);
        Debug.Log(box[0].getElement(0));
    }

    public void actualizarDimensiones(Dimensiones dimension)
    {
        Debug.Log("Width: " + dimension.widht);
        Debug.Log("Height: " + dimension.height);
        box = new List<StringListWrapper>(dimension.widht);
        for(int i = 0; i < dimension.widht; i++)
        {
            box.Add(new StringListWrapper(dimension.height));
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class StringListWrapper
{
    List<string> myList;

    public StringListWrapper(int tama)
    {
        myList = new List<string>(tama);
        for(int i = 0; i < tama; i++)
        {
            myList.Add("");
        }
    }
    public string getElement(int index)
    {
        return myList[index];
    }
}

but when I use

mapa = new Map();
JsonUtility.FromJsonOverwrite(msg,mapa);JsonUtility.FromJsonOverwrite(msg,mapa);

My β€œbox” attribute has nothing. What could be wrong now?

Did you changed the json according to your new structure?

I can’t change the JSON, is a JSON that I have been given and I need to read it. What’s wrong in the JSON?

You changed data structure and now it contains a class. But there’s still array in the json. It will not work. If you really can’t change the json, then i suggest you download Newtonsoft Json plugin and use it instead of unity serializer. But it’s easier to just change json

As I said I can’t change the Json so I will try to use the plugin you said or I will look for something in the asset store.
Thank you

Perfect!, It works Newtonsoft Json. There is an asset in the asset store called β€œjson.net” that implements that library and it works perfect with a List<List>
Thank you!