I’m working with a JavaScript client and a c# socket server sending messages back and forth in JSON.
on the server I have a multi-dimensional array like so:
MapData[0,0,0] = 1;
MapData[0,0,1] = 2;
its dimensions are 900x900x2
in JavaScript if I do json.stringify(MapData) I get like this
[[[1,2],[3,4]]] etc
but c# jsonconvert.serializeobject(MapData) gives me like this:
[1,2,3,4]
can someone explain why the c# serialize and JavaScript stringify are giving different values ?
I tried
int[][][] MapData = new int[900][][];
// Zero out the map
for (int variable1 = 0; variable1 <= 899; variable1++) {
for (int variable2 = 0; variable2 <= 899; variable2++) {
MapData[variable1][variable2][0] = 0;
}
}
but it throws an unhandled null exception at MapData[variable1][variable2][0] = 0;
Finally figured it out with Jeff’s help
int[][][] MapData = new int[900][][];
// Zero out the map
for (int a = 0; a < MapData.Length; a++)
{
MapData[a] = new int[900][];
for (int b = 0; b < MapData[a].Length; b++)
{
MapData[a][b] = new int[2];
for (int c = 0; c < MapData[a][b].Length; c++)
MapData[a][b][c] = 0;
}
}
then the JsonConvert.SerializeObject(MapData) works like I want
Thankyou for pointing me in the right direction Jeff
JsonConvert.SerializeObject()just uses an iterator to get the contents of an array and serialize it. Multidimensional arrays implement IEnumerable, so that’s why you’re seeing that behavior.JsonConvert.SerializeObject()will give you the result you expect given a jagged array, ieMapData[0][0][1].If it’s feasible to change your .NET code to use a jagged array, then that’s what I’d suggest you do. If not, you can do on-the-fly conversion before serializing: