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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示