weixin_33747129 2014-11-02 07:29 采纳率: 0%
浏览 51

序列化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; }
    }
}
  • 写回答

2条回答 默认 最新

  • weixin_33717117 2014-11-02 16:09
    关注

    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}};
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog