序列化Json的2D阵列

I am having difficulty with serialisation. I have an aspx page that uses an ajax call to return objects from the server side.

Desired output

ModelPoints=[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]] (for WebGL)

Actual output ModelPoints={{Points=[1,2,3]},{Points=[1,2,3]},{Points=[1,2,3]},{Points=[1,2,3]}}

The application flow is

Ajax -> Aspx -> WCF -> Aspx -> Ajax

Below are the classes that are being serialized incorrectly. How do i restructure this so that it works? I do not want to parse 18000+ pts more than once, so the output from the server should be the correct format. i.e. I cannot afford to parse it on the client side.

The object name Points comes from the parameter name, how to do this for a 2D array of floats.

The classes below

[Serializable()]
public class ModelPoint
{
    public ModelPoint()
    {
    }
    public ModelPoint(float x, float y, float z)
    {
        _points = new List<Nullable<float>>();
        _points.Add(x);
        _points.Add(y);
        _points.Add(z);
    }
    private List<Nullable<float>> _points;
    public List<Nullable<float>> Points
    {
        get {return _points;}
        set { _points = value; }
    }
}

[Serializable()]
public class Model
{
    private List<ModelPoint> _modelPoints;

    [System.Xml.Serialization.XmlArray("ModelPoints", IsNullable = true)]
    [System.Xml.Serialization.XmlArrayItem()]
    public List<ModelPoint> ModelPoints 
    {
        get {return _modelPoints;}
        set { _modelPoints = value; }
    }
}

In json, square brackets [] denote an array and curly brackets {} denote an object. So the data structure represented by your desired output:

[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

Is a 2 dimensional array of integers. To obtain that in C#, you could use something like:

List<List<int>> model = new List<List<int>>()
model.Add(new List<int>() {1, 2, 3});
model.Add(new List<int>() {1, 2, 3});

or something like:

int[,] model = new int[5, 3] {{1,2,3},{1,2,3}};

You basically only want a two dimensional array to be produced. Assuming you use the DataContractJsonSerializer on your WCF endpoint your Model class should look like this:

public class Model
{
    private float[][] _modelPoints;

    public float[][] ModelPoints
    {
        get { return _modelPoints; }
        set { _modelPoints = value; }
    }
}

Notice that I removed all the serialization attributes (if you leave them on the serializer magically outputs the backingfield instead of the property) and I have no longer used the ModelPoint class. If I feed that the DataContractJSONSerializer I get your desired output:

var ds = new DataContractJsonSerializer(typeof(Model));

var model = new Model
    {
        ModelPoints = new[] { 
            new[] { 1f, 2f, 3f }, 
            new[] { 4f, 5f, 6f } }
    };
var sb = string.Empty;
using (var ms = new MemoryStream())
{
    ds.WriteObject(ms, model);
    sb= Encoding.UTF8.GetString(ms.ToArray());
}
Debug.WriteLine(sb);

gives in the debug console which is similar to what you expect to receive.

{"ModelPoints":[[1,2,3],[4,5,6]]}