How to deserialize two-dimensional array json

49 Views Asked by At

I have a two-dimensional array json

[
    [{"x":35.77778,"y":206.8565,"z":0},{"x":80.5,"y":206.8565,"z":0}],
    [{"x":35.77778,"y":206.8565,"z":0},{"x":80.5,"y":206.8565,"z":0}]
]

I have a C# class

public class Position 
{
    public float x { get; set; }
    public float y { get; set; }
    public float z { get; set; }
}

Now I want to convert the json to Position[,]. I'm using LitJSON, so I write like this.

string s = "[ [{ \"x\":35.77778,\"y\":206.8565,\"z\":0},{ \"x\":80.5,\"y\":206.8565,\"z\":0}], [{ \"x\":35.77778,\"y\":206.8565,\"z\":0},{ \"x\":80.5,\"y\":206.8565,\"z\":0}] ]";
Position[,] pss = JsonMapper.ToObject<Position[,]>(s);

But it's wrong, error is "Position can't act as an array".

JsonMapper.ToObject can do correct in one-dimensional array json. For example

string s = "[{ \"x\":35.77778,\"y\":206.8565,\"z\":0},{ \"x\":80.5,\"y\":206.8565,\"z\":0}]";
Position[] ps = JsonMapper.ToObject<Position[]>(s);

So I ask how to deserialize two-dimensional array json in c# by using LitJSON ?

1

There are 1 best solutions below

0
On

Try using a "jagged" array:

Position[][] pss = JsonMapper.ToObject<Position[][]>(s);