C# How save data from multidimensional array to a json

this is my first thread, so the formating is probably wrong

So, i have this big public class called Ram with my gigantic array called map. it looks like this:

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

public class Ram
{
public static int[,] map;

public static int mapSize;

}

karte is defined in another script like this:

void HexSkizze1()//create empty Hexmap
{
int Zone = 7, category= 10;
//Erstellen Karten Array
Ram.map = new int[Ram.mapSize, Ram.mapSize, Zone, category];
}

here’s a little explanation:

Ram.map [ X 215 , Y 412 , Zone 0 , category 0 ] = 4; // is the basic hight of the whole grid
Ram.map [ X 135 , Y 232 , Zone 2 , category 3 ] = 0; // is the type of conection prefab nessesary to connect to its right zone 3 neighbour by 10 m

i am prossesing hexagons with its borders to their neighbours thats why its 7 zones per grid

anyway, since my maps are currently created in a generator, i want to be able to teraform and want to be able to save the tarain.

so, how can i convert Ram.map into a JSON file with a format like this:

“X:0,Y:0,Zone:0,Category:0”:“5”,
“X:0,Y:0,Zone:0,Category:1”:“2”,
“X:0,Y:0,Zone:0,Category:2”:“0”,
“X:0,Y:0,Zone:0,Category:3”:“8”,

maybe somebody has a better format idea
thanks in advance

btw here is how it looks:


Welcome to the forum! For better readability, just put your code into the code field. You can do that by selecting an icon to the right of the “Code:” in the text field edit bar.

Your screenshots look really nice. Do you use your own generator or is it an asset? If you are using an asset, maybe provide a name, as i’m sure there might be other Unity developers who have experience using it and might help you out.

Converting any data to JSON requires you to serialize it. Serialization and deserialization of JSON is simply put a process of “converting” data into JSON. The quickest and the easiest way to do that is to use a library. Unity provides a class for that, called JSONUtility (Unity - Scripting API: JsonUtility), more on serializing the data can be found here: Unity - Manual: JSON Serialization. However, Unity themselves admit that for things like unstructured JSONs you should seek an alternative (“If you need to do this, you should look for a more fully-featured JSON library.”). I’d reccomend using GitHub - applejag/Newtonsoft.Json-for-Unity: Newtonsoft.Json (Json.NET) 10.0.3, 11.0.2, 12.0.3, & 13.0.1 for Unity IL2CPP builds, available via Unity Package Manager to also gain a better multiplatform compatibility, as JSON can be finnicky on some platforms from what i recall reading about it (e.g Android).

To organize your data into structures that you can “feed” into JSON serializer/deserializer, you’ll have to build a constructor. Here is an example of using a constructor to “feed” the data into creating objects of the constructor class ( Index variable not doing what it is supposed to do. ).

I don’t want to intervene in your original intent, however why do you “lump” X, Y, Zone and Category together? It would make it a bit easier if you separated all four into their individual name-value pairs.

If you seek alternatives to JSON, a binary might be a solution, however it’s not as easy to edit as a well laid out JSON file.