加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用嵌套类创建类的Javascript JSON

发布时间:2020-12-15 17:21:17 所属栏目:百科 来源:网络整理
导读:我想在 JavaScript中创建一个具有嵌套对象的JSON对象. 以下是课程: public class CellChanged{ private CellLocation _Location = null; private double _CellValue = 0; public CellLocation Location { get { return this._Location; } set { this._Locat
我想在 JavaScript中创建一个具有嵌套对象的JSON对象.

以下是课程:

public class CellChanged
{
    private CellLocation _Location = null;
    private double _CellValue = 0;

    public CellLocation Location
    {
        get
        {
            return this._Location;
        }
        set
        {
            this._Location= value;
        }
    }

    public double CellValue
    {
        get
        {
            return this._CellValue;
        }
        set
        {
            this._CellValue = value;
        }
    }

}


public class CellLocation
{

    #region Members

    private int _Worksheet = 0;
    private int _Row = 0;
    private int _Column = 0;
    private string _CellName;

    #endregion //Members

    #region Properties

    public int Worksheet
    {
        get
        {
            return this._Worksheet;
        }
        internal set
        {
            this._Worksheet = value;
        }
    }

    public int Row
    {
        get
        {
            return this._Row;
        }
        internal set
        {
            this._Row = value;
        }
    }

    public int Column
    {
        get
        {
            return this._Column;
        }
        set
        {
            this._Column = value;
        }
    }

    public string CellName
    {
        get
        {
            return this._CellName;
        }
        internal set
        {
            this._CellName = value;
        }
    }

    #endregion //Properties

    #region Constructors

    internal CellLocation()
    {

    }

    public CellLocation(int worksheet,string cellName)
    {
        this.Worksheet = worksheet;
        this.CellName = cellName;
        int i = 0;
        string columnRaw = String.Empty;
        string rowRaw = String.Empty;
        int column = 0;
        int row = 0;
        while (Char.IsLetter(this.CellName,i))
        {
            columnRaw += this.CellName.Substring(i,1);
            i++;
        }
        column = Utilities.Excel.ColumnLetterToNumber(columnRaw);
        rowRaw = this.CellName.Substring(i);
        if (!Int32.TryParse(rowRaw,out row))
            throw new ApplicationException(String.Format("Cell name {0} is invalid",cellName));

        this.Row = row - 1;
        this.Column = column;
    }

    [JsonConstructorAttribute]
    public CellLocation(int worksheet,int row,int column)
    {
        this.Worksheet = worksheet;
        this.Row = row;
        this.Column = column;
        //set the cell name
        this.CellName = String.Concat(Utilities.Excel.ColumnNumberToLetter(column),row + 1);
    }

    #endregion //Constructors

}

这是我想要输出的最终字符串:

"{"Location":{"Worksheet":1,"Row":2,"Column":3},"CellValue":4.5}"

这样做的正确方法是什么?

如果重要,我在后端使用Newtonsoft JSON库.

解决方法

这比我想象的容易得多.

这是JavaScript:

var cellChanged = {
    "Location": {
        "Worksheet": workSheetCurrent,"Row": row,"Column": column
        },"CellValue": cellValue
};

在服务器端,使用Newtonsoft JSON库进行反序列化甚至更为简单:

CellChanged cell = JsonConvert.DeserializeObject<CellChanged>(context.Request["CellChanged"]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读