#How to get multi-dimensional array from JSON

1 messages · Page 1 of 1 (latest)

icy slate
#

Hello, I have a JSON file that looks something like this (pic 1). I want to pull all that data into unity, so I've created a class that represents the way the data is serialised

public class Department
    {
        public int id;
        public string name;
        public string goal;
        public int semesters;
        public string[] towns;
    }

normally, if I just had an array of department objects I'd do something like

[System.Serializable]
    public class DepartmentList
    {
        public Department[] test_1;
    }

    public DepartmentList myDepartmentList = new DepartmentList();
    
    void Start()
    {
        myDepartmentList = JsonUtility.FromJson<DepartmentList>(textJSON.text);
    }

However given that my department objects are actually deep inside a maze of arrays, how would I go about getting that data?
I've already tried declaring a multidimensional array in the DepartmentList class like

//kept adding and removing commas in case it worked, it didn't
public class DepartmentList
    {
        public Department[,,,] test_1;
    }

but that didn't work, so I'm open to ideas

#

Expert representation of what my data should look like

short fog
#

I would probably use something like Newtonsoft JSON.Net instead of Unitys built-in JsonUtility, simply because I dont think JsonUtility can serialize collections and deep nested data like that, while other serializers should be able to, Newtonsoft is just my personal favorite choice

Other than that, looking at your last picture, it sounds like what you want is a List of all your data instead of arrays, it sounds like your data should look something like this? Correct me if I may be misunderstanding:

public List<AcademicDirection> directions;

public class AcademicDirection
{
public string name;
public List<Field> fields;
}

public class Field
{
public string name;
public List<Department> departmets;
}

public class Department
{
public int id;
//...
}
icy slate
#

I'll look into it and update the thread when I reach something, thanks!

#

One little thing @short fog, Academic direction, field etc don't really have a name. They are a bunch of nested arrays with no need for a name

#

It's 7AM, I've been up all day. I'll catch some Zs and update the thread tomorrow, I don't want to accidentally drop any misleading information 😅

icy slate
#

Alright, I'm fresh now. Yes I used to have nested json objects (which is what I assume you mean by list) instead of nested arrays, however I thought that making everything a system of arrays would lead to much simpler code. If your code however works with objects I'd be more than happy to refactor mine back to its original state

icy slate
#

here's a snippet of my JSON as it stands now (feel free to ignore the values)

icy slate
#

and here's a snippet of what I think you recommend I do?

short fog
# icy slate and here's a snippet of what I think you recommend I do?

If there are different "directions" and "fields" that hold a different set of data, then I think lists makes sense, if your data is only what is contained in "department" and "field" and "directions" are nothing more than labels, then you could probably make them a enum you assign to departments, otherwise, I may not fully understand their purpose of nesting your data further

icy slate
#

hello, ended up feeding it to the demon (chatgpt) and it gave me some sweet answers 😅 thank you for your time dibble!

short fog
#

Np, good to hear you got it worked out though lol, seems like ChatGPT has been getting quite popular for programming questions