0

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 ?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
hellozjf
  • 169
  • 5
  • 16
  • Did you try to serialize a 2d array and look at the output and verify if there is a difference in the json syntax? – Rand Random Nov 29 '22 at 17:17

1 Answers1

3

Try using a "jagged" array:

Position[][] pss = JsonMapper.ToObject<Position[][]>(s);
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Charles Yang
  • 330
  • 2
  • 10